#so for the morph game im making i added a hitbox to one of them but its kinda broken

1 messages · Page 1 of 1 (latest)

tight pivot
#

I added a script that when you press the 1 key a remotevent fires a server script and when the server script is fired whoever is inside the hitbox gets hurt and then waits a cooldown, but for some reason the hitbox keeps turning on even when I dont press one, I added a print and it turns out the script just keeps running every 7 seconds

this is the server script:

local humanoid = script.Parent.Parent:WaitForChild("Humanoid")
local biteAnim = script.Parent.Parent["whale anim"].Bite
local debounce = true
local Anim = humanoid:LoadAnimation(biteAnim)
local time = Anim.length
local remote11 = game.ReplicatedStorage.RemoteEvent11

remote11.OnServerEvent:Connect(function()
game:GetService("RunService").Heartbeat:Connect(function()
for _, v in pairs(workspace:GetPartsInPart(script.Parent)) do
if v.Parent:FindFirstChild("Humanoid") then
if debounce == true then
debounce = false
script.Parent.CanTouch = true
print("it worked")
v.Parent.Humanoid:TakeDamage(100)
wait(time)
task.wait(7)
debounce = true
end
end
end
end)
end)

pale hatch
#

When you use RunService.Heartbeat, you are basically running the code inside over and over (like a while loop), so to fix this just delete the Heartbeat line

left flameBOT
#

studio** You are now Level 1! **studio

tight pivot
#

oh

#

ok thank you

tight pivot
#

so the script still loops somehow

#

local humanoid = script.Parent.Parent:WaitForChild("Humanoid")
local biteAnim = script.Parent.Parent["whale anim"].Bite
local debounce = true
local Anim = humanoid:LoadAnimation(biteAnim)
local time = Anim.length
local remote11 = game.ReplicatedStorage.RemoteEvent11

remote11.OnServerEvent:Connect(function()
for _, v in pairs(workspace:GetPartsInPart(script.Parent)) do
if v.Parent:FindFirstChild("Humanoid") then
if debounce == true then
debounce = false
script.Parent.CanTouch = true
print("it worked")
v.Parent.Humanoid:TakeDamage(100)
wait(time)
task.wait(7)
debounce = true
end
end
end
end)

#

this is what it looks like now

tight pivot
pale hatch
#

what does your LocalScript look like?

tight pivot
#

let me get it

left flameBOT
#

studio** You are now Level 2! **studio

tight pivot
#

plr = game.Players.LocalPlayer
local character = script.Parent
local remote11 = game.ReplicatedStorage:WaitForChild("RemoteEvent11")
local humanoid = character.Parent:WaitForChild("Humanoid")
local UserInputService = game:GetService("UserInputService")
local biteAnim = script.Parent.Bite
local debounce = true
UserInputService.InputBegan:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.One then
if debounce == true then
debounce = false
remote11:FireServer()
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Anim = humanoid:LoadAnimation(biteAnim)
local time = Anim.Length
print("spino is goat")
Anim:Play()
task.wait(time)
Anim:Stop()
task.wait(7)
debounce = true
end
end
end)

pale hatch
#

you only need the debounce once - either the client or server, so i would remove the debounce code from the server, otherwise you may have a race condition (client or server finishes first and the wait is misaligned)

tight pivot
#

oh ok

#

btw the code loops two times before stopping

#

like the server script does too

#

well the script still repeats two times

#

I removed debounce from the local script

tight pivot
#

it still repeats two times

#

are you sure there isnt any other reason on why the code might repeat two times?

#

maybe do you think it has anything to do with the for loop

pale hatch
#

oh you are right, just insert a break after task.wait(7) in the server

#

basically there must be 2 parts in script.Parent that are in the box, so it runs for both, so you need to break the loop once it triggers for a part

tight pivot
#

oh

tight pivot
#

im kind of new so

pale hatch
#

just type the word break

tight pivot
#

oh like right under the task.wait?

pale hatch
#

yes

tight pivot
#

is this good>

pale hatch
#

fix the indentations for the ends

#

the end after the break should be after the debounce and indented correctly i think

tight pivot
#

ok let me chec kthat

#

says i have to add an end at line 11

#

but when i add an end it just adds more errors

#

ok so i fixed the indentations

#

this is whta it looks like currently

#

one thing that bothers me tho is that the print("it worked") prints 2 times so idk if thats supposed to happen or not

left flameBOT
#

studio** You are now Level 3! **studio

pale hatch
#

it should only print once

left flameBOT
#

studio** You are now Level 3! **studio

tight pivot
#

oh

#

well do u think theres any problem with the script

#

why it might do that

pale hatch
#

does it print 2 immediately?

tight pivot
#

yes

pale hatch
#

is there just 1 server script?

tight pivot
#

i mean

#

theres an animation script for the morph

#

but I dont think it has to do with this

#

it only works with like the idle

#

and walk animations

pale hatch
#

is that wait(time) meant to be there?

tight pivot
#

yeah it waits the amount of time the animation lasts

#

the bite animation

pale hatch
#

time is a built-in syntax, so you should change the time variable to something like animLength

tight pivot
#

oh oaky

#

do u think that will fix the problem of it printing twice

pale hatch
#

probably not but it clears things a little

tight pivot
#

oh ok

#

alright i did it

#

this is what it does btw

#

when i press the one key

pale hatch
#

can you also add print(debounce) under the current print statement and run again

tight pivot
#

like the "it worked"?

#

where should i put the print statement

pale hatch
#

yes under that

tight pivot
#

ok

#

this is the result

#

this is the code btw

pale hatch
#

sorry, i meant add print(debounce) instead of print("it worked but v2")

#

so print the variable called debounce

#

i want to see the value of debounce in each run

tight pivot
#

ok

pale hatch
#

oh can you also add print("got event") as the first line (above the for loop)

tight pivot
#

ok

#

this is what it print out

pale hatch
#

ok great

#

now can you show me your explorer (around where your script is)

tight pivot
#

ok

#

here

pale hatch
#

what is the model?

tight pivot
#

its a rig

#

let me show u

left flameBOT
#

studio** You are now Level 4! **studio

pale hatch
#

ah

#

it's because there are two

#

it runs in each whale

tight pivot
#

oh

pale hatch
#

it seems like the code above should be in it's own script inside ServerScriptService

tight pivot
#

hey so

#

i tried to say uhm s whale

#

but u cant say the first word

#

but i was trying to say

#

do you think the script would still break if there are multiple whales in one game

#

because this is meant to be like a online fish game

pale hatch
#

if you have this code inside each whale, whoever is in the box when you press 1 will take 100 damage multiplied by how many whales you have

tight pivot
#

aw

#

is there any way to fix taht

pale hatch
#

is your plan to have a hitbox in the mouth of each whale?

tight pivot
#

yeah kinda

pale hatch
#

ok so first of all, move the remote event code into its own server script inside ServerScriptService

#

that way it will only fire once every time an event is fired

tight pivot
#

so do i move the server script into serverscriptservice

pale hatch
#

yes

left flameBOT
#

studio** You are now Level 4! **studio

tight pivot
#

I really hope this works

pale hatch
#

then pass the whale model as a parameter when you fire an event

tight pivot
#

thank you for helping me

#

ok

pale hatch
#

then you can still handle the animation of the whale via that script

tight pivot
#

this good?

pale hatch
#

sorry not the client script

#

i meant the Bitehitbox script

tight pivot
#

oh ok

#

this better?

pale hatch
#

yes

#

of course all the code that has script.Parent won't work anymore

tight pivot
#

oh ok

#

do i just wire it up to the whale

pale hatch
#

youll have to get the humanoid and biteAnim etc from the whale model that you passed as a parameter

tight pivot
#

alr

pale hatch
#

so in your local script when you press 1, do remote11:FireServer(whale)

#

then in the server script you need to define them in the function parameters (e.g., function(player, whale))

tight pivot
#

do i have to put the name of the fucntion

#

or exactly what you put

#

this good

#

oh weait

#

the remote event

#

I have a question

#

exact;ly what am i putting inside the parameters

#

hello?

pale hatch
#

sorry ill brb

tight pivot
#

oh ok

pale hatch
#

wait actually if the player’s character is the whale when the remote event is fired, then you don’t need to pass anything into FireServer

tight pivot
#

oh then what do i do

pale hatch
#

you can get the whale in the server script by just doing player.Character

#

show me your server script

tight pivot
#

whic h one

pale hatch
#

Bitehitbox

tight pivot
#

oh okay

pale hatch
#

first of all remove all the debounce code from that code

tight pivot
#

ok

left flameBOT
#

studio** You are now Level 5! **studio

tight pivot
#

ok

#

i didi it

pale hatch
#

move all the code at the top to above the for loop

#

and remove all print statements

tight pivot
#

ok

pale hatch
#

replace function() with function(player)

tight pivot
#

ok

pale hatch
#

sorry move the remote11 variable back to the top of the script

tight pivot
#

theres is an error at the bottom

pale hatch
#

remove the end below the break

tight pivot
#

ok

pale hatch
#

add the line:
local char = player.Character
above the humanoid variable

#

set the humanoid variable to char:FindFirstChildOfClass("Humanoid")

tight pivot
pale hatch
#

then under the humanoid variable:
if not humanoid then return end

tight pivot
#

wgere

#

what line

pale hatch
#

below the humanoid variable

tight pivot
#

mkay

pale hatch
#

then set biteAnim to char["whale anim"].Bite

tight pivot
pale hatch
#

add the lines following line under animLength:
local hitbox = char:FindFirstChild("bitehitbox")
if not hitbox then return end

tight pivot
pale hatch
#

then replace workspace:GetPartsInPart(script.Parent) with workspace:GetPartsInPart(hitbox)

#

and that should be it

tight pivot
#

ok let me check

#

this is good right

pale hatch
#

that will work when you have morphed into the whale then press 1

tight pivot
#

oh uhm i pressed one but it wot work

pale hatch
#

no error?

tight pivot
#

no errors

pale hatch
#

no print statements?

tight pivot
#

not from this script i think

#

nope

pale hatch
#

show me the localscript again

tight pivot
pale hatch
#

so when you press 1 you dont even see "got event"?

tight pivot
#

nope

#

sorry wrote it two times

#

but its as if the script isnt even there

pale hatch
#

add print("hello") at the top of the server script

tight pivot
#

alr

#

it owrks

#

just that efverything else doesnt

pale hatch
#

does spino is goat get printed?

tight pivot
#

no

pale hatch
#

where did you move the bite (local script) to?

tight pivot
pale hatch
#

oh, it needs to be in StarterPlayerScripts

left flameBOT
#

studio** You are now Level 5! **studio

tight pivot
#

oh okay

#

by the way

#

if i do this

#

will i be able to add more morphs

#

other than the whale

pale hatch
#

yes

#

but you will need to update the server script

tight pivot
#

oh how

pale hatch
#

so it's important that you understand the changes i asked you to make in the server script

tight pivot
#

oh

#

what chagnes did you ask me to make

#

like specifically what did they do

pale hatch
#

so the first thing i asked was to move all those lines that had script.Parent into the function

#

incase you didn't know, script.Parent returns the parent of the script

#

before you moved Bitehitbox script, it's parent was the bitehitbox Part inside the whale model, remember?

tight pivot
#

yeah

pale hatch
#

so when you moved the Bitehitbox script into ServerScriptService, script.Parent would return ServerScriptService

tight pivot
#

oh yeah that would make sense

#

the parent is the one holding the script

pale hatch
#

yes

#

and we also added the player as a parameter to the function

tight pivot
#

oh btw the bite still doesnt work

#

nothing gets printed

pale hatch
#

where is the bite script?

tight pivot
pale hatch
#

at the top of the bite script, add print("hello")

tight pivot
#

ok

#

works

pale hatch
#

below local debounce = true, add print("got debounce")

tight pivot
#

so

#

it doesnt print

pale hatch
#

no errors?

tight pivot
#

oh wait

#

no

#

but i think i messed up the character variable

#

let me go check

pale hatch
#

it looks ok

tight pivot
#

are you sure?

pale hatch
#

put a print statement under each variable at the top to see where the code stops

tight pivot
#

look at the humanoid variable

#

ok

#

stops at two

pale hatch
#

youre right its the humanoid then

tight pivot
#

alr let me fix that

#

lets see if that workeed

left flameBOT
#

studio** You are now Level 6! **studio

tight pivot
#

yup

#

it fixed

pale hatch
#

nice

tight pivot
#

but somehow

#

it still doesnt work

#

when i press one

#

nothing happens

pale hatch
#

no server prints?

tight pivot
#

oh wow

#

uhm

#

so

#

this animation on the morphg plays

#

but not on the player

pale hatch
#

so it doesnt work for the player's whale

tight pivot
#

yup

pale hatch
#

when does the player morph into the whale?

tight pivot
#

when it touches the morph box

pale hatch
#

ah

#

move the script into StarterCharacterScripts and we'll make a few changes

tight pivot
#

ok

#

alr i did it

pale hatch
#

set the character variable to plr.Character

tight pivot
pale hatch
#

does the whale need to be under Model1?

tight pivot
#

yeah

pale hatch
#

apart from S whale, what else is under Model1?

tight pivot
#

the moprh box

#

I didnt know how to implement it into the gui

#

so i turned it into a touched event

pale hatch
#

ok that's fine

tight pivot
#

after following a tutorial

pale hatch
#

ignore that

tight pivot
#

alright

pale hatch
#

assuming you set the character variable to plr.Character

tight pivot
#

i did

pale hatch
#

one sec

tight pivot
#

ok

pale hatch
#

can you add the line: print(character.Name) under the character variable and do a test run?

#

it should print twice

tight pivot
#

ok

pale hatch
#

once when you spawn and again when you morph

tight pivot
#

dont see anything

pale hatch
#

sorry when i said set the character variable to plr.Character, i meant the variable at the top

tight pivot
#

oh okay

pale hatch
#

move the print statement to the character at the top, too

#

you can just remove the variables Player and Character inside the function

tight pivot
#

is this good?

pale hatch
#

thats right

tight pivot
#

mkay

#

what now

#

works

pale hatch
#

did you morph?

left flameBOT
#

studio** You are now Level 6! **studio

tight pivot
#

yesh

#

wait

#

let me try again

#

oh no i didnt

#

it just does it

#

on spawn

pale hatch
#

yeah but does it print again when you morph?

tight pivot
#

let me cehck

#

no

#

sadly

pale hatch
#

ok thats fine

#

where is the morph script located?

tight pivot
#

inside the mode;l

pale hatch
#

move the bite script into ServerStorage

tight pivot
#

ok

pale hatch
#

at the top of the morph script, add the line: local biteScript = game.ServerStorage:WaitForChild("bite")

#

then before player.Character is set to the whale during the morph, add the line: local biteScriptClone = biteScript:Clone()

#

then below that add biteScriptClone.Parent = whale (or whatever the whale clone is set to)

tight pivot
pale hatch
#

show me the script

tight pivot
pale hatch
#

yeah so above plr.Character = charClone

#

then show me again just to confirm

tight pivot
pale hatch
#

sorry, add the lines i wrote above, above plr.Character = charClone in the function near the bottom

#

remove that top line

tight pivot
#

ok so

#

exactly what am i adding

pale hatch
#

local biteScriptClone = biteScript:Clone()

#

biteScriptClone.Parent = charClone

tight pivot
#

and in the function

#

inside of it

pale hatch
#

you have function that is assigned to a variable called rizz

#

inside that function, put those lines above plr.Character = charClone

tight pivot
pale hatch
#

correct

#

now test again

#

you should see the print statements from the bite script after you morph

#

wait no

tight pivot
#

yeah

#

i dont see thenm

pale hatch
#

you didnt add the line biteScriptClone.Parent = charClone

tight pivot
#

oh ok

pale hatch
#

under the biteScriptClone variable

#

sorry show again

tight pivot
#

kinda looks wrong

pale hatch
#

move those 2 lines directly above plr.Character = charClone

#

and remove the parentheses after charClone

tight pivot
#

wheres that

pale hatch
#

can you see the line plr.Character = charClone?

tight pivot
#

let me see

pale hatch
#

3rd line from the bottom in that screenshot

tight pivot
pale hatch
#

perfect

tight pivot
#

it works

#

only prints after i morph

#

which is supposed to happen right

pale hatch
#

yes

#

does your animation work?

tight pivot
#

the bite doesnt

pale hatch
#

look at Bitehitbox again

tight pivot
pale hatch
#

the server script

tight pivot
#

oh ok

pale hatch
#

it looks like theres still a script.Parent in it

tight pivot
#

oh might be

pale hatch
#

just remove that line

tight pivot
#

ok

pale hatch
#

script.Parent.CanTouch = true

tight pivot
#

the animatioon doesnt play

#

but the humanoid gets hurt

#

so the take damage is taken into affect

pale hatch
#

you also need to do Anim:Play()

tight pivot
#

where

pale hatch
#

right below the Anim variable

tight pivot
#

ok

#

where here?

pale hatch
#

no the server script

#

line 9 in Bitehitbox

tight pivot
#

ok

#

it loops

pale hatch
#

the bite animation loops over and over?

tight pivot
#

yes

pale hatch
#

above Anim:Play, add Anim.Looped = false

tight pivot
#

works

#

real good

pale hatch
#

congrats

tight pivot
#

does this mean i can uh

#

remove the task.waits

#

that wait for the animation lenght

#

i only added those so that the cooldown would be its own thing

left flameBOT
#

studio** You are now Level 7! **studio

tight pivot
#

or do i still keep it to make the cooldown seven seconds

#

not including animation length

pale hatch
#

on the server script you can remove the task.wait(7) because nothing happens after it

tight pivot
#

oh then how do i implement the uhm

#

cooldown

#

so that i can also wire it up to gui

pale hatch
#

the cooldown is on the client

tight pivot
#

oh ok

pale hatch
#

thats what the debounce variable is for

#

we removed it from the server

tight pivot
#

mkay

#

i removed the seven seconds

pale hatch
#

also in the client script (bite), you are also running the bite animation

#

try removing the animation code from the local script and confirm that it still works

tight pivot
#

ok let me go try

#

ok it works

pale hatch
#

so you can also remove Anim, animLength and task.wait(animLength)

tight pivot
#

ok it works

pale hatch
#

please try and understand all the code and why each line is there, otherwise it will be difficult to implement other morphs and other parts of the game

tight pivot
#

ok

#

but question

#

uhm

#

can yuo give me like a bit of a hint on how to add other morphs

#

im still a bit confused because theres a server and client script

pale hatch
#

do you know what is different between a server and client script?

tight pivot
#

uhm yeah

#

i know one is used for

#

local stuff

#

like

#

uhhhhhh

#

gui

#

and

#

i dont remember what eelse

pale hatch
#

yes, but in general, local scripts are run on the user's computer/phone/console

#

whereas the server script is run in the game's server (somewhere random in the world)

tight pivot
#

oh ok

pale hatch
#

so the localscript has access to all the player stuff (like gui)

#

BUT the localscript cant access ServerStorage or ServerScriptService

#

and the localscript only runs if inside the player's character or inside PlayerScripts

tight pivot
#

oh

#

ok

pale hatch
#

if it's directly under the workspace, it wont run

#

that's why youre using a remove event in the first place

#

so you can tell the server to do stuff from the client

#

if you try to damage another player from a localscript, it won't work

tight pivot
#

oh

#

so to add another character would i have to add them in the same script

#

or make a new one

pale hatch
#

show me the morph script again

tight pivot
pale hatch
#

if you were a bad developer, you could just copy the script into every other morph you want

left flameBOT
#

studio** You are now Level 7! **studio

tight pivot
#

thats exactly what i was gonna do is there a more proffesional way?

pale hatch
#

you should have a single script in ServerScriptService again

#

and I would use this thing called tags

tight pivot
#

what are tags

pale hatch
#

basically, you can run code for various types of instances with different tags

#

it's hard to explain, but it's useful if you don't want to put a script into every morph in the workspace

tight pivot
#

oh

#

well like

#

would i put a tag in every morph

pale hatch
#

you would create a tag called, say "Animal" or "Fish" or whatever your category is

tight pivot
#

mhm

pale hatch
#

and add this tag to every morph box

#

actually you would probably call the tag "AnimalMorph" to be more accurate

#

i would look at a tutorial on how to use tags, since we're getting out of the scope of this ticket thingy

tight pivot
#

oh okay

#

so would i put a tag

#

on the script

#

and then a tag on every morph?

pale hatch
#

in the script you would use the service called CollectionService (game:GetService("CollectionService"))

#

and if you look at the properties of any instance in the workspace and scroll to the bottom, you can see that you can add Tags

tight pivot
#

yeah

#

i saw that

#

so one script

#

but multiple morhps

pale hatch
#

yes

#

look at a tutorial since i can't explain it all here

tight pivot
#

ok

#

btw

#

one more question

#

how do you make the attack not hurt the local palyer

#

and only other people

pale hatch
#

show me the server script again

tight pivot
#

alr

pale hatch
#

see how you get the humanoid in the for loop

tight pivot
#

y6es

pale hatch
#

do you know what v.Parent will be?

tight pivot
#

uhm

pale hatch
#

since it's also the parent of the humanoid

tight pivot
#

the parent of whom got hit

pale hatch
#

just to confirm, you only want players to get hurt, not npcs or anything else with a humanoid?

tight pivot
#

i do want npc to get hurt

pale hatch
#

ok thats fine

tight pivot
#

im gonna have npc fishs for new players

pale hatch
#

so to answer my question above, if humanoid is v.Parent's child

tight pivot
#

oh

pale hatch
#

then v.Parent is the character

tight pivot
#

would i have to get the name

pale hatch
#

no

#

easier

#

you can do game.Players:GetPlayerFromCharacter(v.Parent)

#

if it returns a player, then you can check that the player doesn't equal the player passed in to the function

tight pivot
#

so would that be like

#

if game.Players:GetPlayerFromCharacter(v.Parent) ==

#

then uh

#

i dont know whtas next

pale hatch
#

on the right side should be player

tight pivot
#

oh

#

just player

pale hatch
#

inside the then, add continue

tight pivot
#

oh

#

would that not hurt the humanoid?

pale hatch
#

if it is equal to the player that fired the server, then it should continue and not hurt them

tight pivot
#

oh

#

it would hurt dummys tho right

pale hatch
#

if you add the continue keyword in a for loop, it skips the rest of the code and runs the next iteration

#

yes

tight pivot
#

alr

#

let me try that

#

this is good right

pale hatch
#

thats right

tight pivot
#

ok let me try

#

IT WORKSSSS thank you sooo much

pale hatch
#

nice

tight pivot
#

by the way

#

this should work online

#

right

pale hatch
#

yes

tight pivot
#

like if i get my friend we could fight and it should work

#

okay

#

ur a life saver

#

oh my gosh

pale hatch
#

no problem

tight pivot
#

@pale hatch

#

hey question

#

you know the animations right

#

yeah so uh they wont play

#

in a server

pale hatch
#

oh that means there’s a permission issue with the animation

tight pivot
#

oh

#

how do you fix that

#

sorry for uh responding late again

#

@pale hatch you there

tight pivot
tight pivot
#

@pale hatch hey sorry to bother but are you there?

pale hatch
#

if you google the issue (animation works in roblox studio but not in game) then you should fine the solution, since i had to do the same thing

tight pivot
#

oh ok