Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What causes all my variables I create to be nil whenever called in a function?

In my script for when my player dies any time I use a variable in a function it Errors out and causes my entire script to stop working like this

local human = character:FindFirstChild("Humanoid")

human.Changed:connect(function(death) 
if human.Health == 0 then 

then I get this http://gyazo.com/506bf5c20756e130e337ccb3e5f7bf7c <-- picture

the script works completely correct in play solo just not online

3 comments

0

whats the if statement doing?

1

just checking to see if the player really died

0

You could use the Died event instead so that the check is unnecessary. It might also catch a few death-causes that don't fire the Changed event, though I'm not sure. Can you include the rest / more of the script? We need more context I think. Also, can you indicate for sure which line is "line 4" in your actual code?

1 answer

✓ Accepted answer

Try using WaitForChild instead of FindFirstChild. If FindFirstChild doesn't return the humanoid, it will cause an error as shown in your script, where as WaitForChild will wait until the child is available.

Also, I recommend using the Died event instead of waiting for the health to change. (Unless you need it to check for the health to be changed for some reason. In that case, ignore what I said.)

local human = character:WaitForChild("Humanoid")

human.Died:connect(function() 
	--Enter code here
end)

1 comment

0

OMG THANK YOU SO FREAKING MUCH I'VE BEEN TRYING TO FIX THIS SCRIPT FOR 3 DAYS NOW AND NOBODY COULD HELP ME!!!

This discussion is preserved as it was. New answers, comments, and votes are closed.