Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by
Prioxis 673 Moderation Voter
9 years ago

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

0
whats the if statement doing? NinjoOnline 1146 — 9y
1
just checking to see if the player really died Prioxis 673 — 9y
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? BlueTaslem 18071 — 9y

1 answer

Log in to vote
1
Answered by 9 years ago

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)
0
OMG THANK YOU SO FREAKING MUCH I'VE BEEN TRYING TO FIX THIS SCRIPT FOR 3 DAYS NOW AND NOBODY COULD HELP ME!!! Prioxis 673 — 9y
Ad

Answer this question