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

Script says humanoid is nil? Probably looking before it's loaded?

I assume its looking for the humanoid in the character before the character has loaded into the game yet despite what I try, it always comes up nil. How would I go about waiting for the character to load before accessing it? I tried WaitForChild that says infinite yield for Character, which I didn't think was a problem yet no other error comes up and it does not work.

local p = game.Players.LocalPlayer
local gui = script.Parent.Stats.Health
local c = p.Character
local h = c.Humanoid

while true do
	wait()
	gui.Text = "Health " .. h.Health .. "/100"
end

Error is on the third like saying it's nil. Any help is appreciated :)

2 answers

✓ Accepted answer

Character is not a "Child" of player

Character is a property, not a child. To wait for it, simply wait for the event CharacterAdded to fire. I would suggest using more WaitForChilds on the other stuff as well. Other than that, I see nothing wrong.

local p = game.Players.LocalPlayer
local gui = script.Parent:WaitForChild("Stats"):WaitForChild("Health")
local c = p.Character or p.CharacterAdded:wait()
local h = c:WaitForChild("Humanoid")

while true do
	wait()
	gui.Text = "Health " .. h.Health .. "/100"
end

Let me know if that works for you.

If I helped, please don't forget to accept my answer.

Is filtering enabled on? If so, I'm pretty sure your script won't work.

To use local scripts with FE (Filtering Enabled) on, you need to connect server scripts to local scripts using Remote Events. Or, you can just turn FE off, if you want to be unsafe.

Hope I helped you out!

6 comments

0

What an absolutely "poor" answer. I don't think you understand what FE does.

0

Yes, you're right, I don't. I included "I'm pretty sure" because I'm just a beginner scripter. I just have an intention to help.

0

Understand something before trying to teach it.

0

I removed my downvote because I understand how harsh people this this site can be, including me. This answer was wrong though, and that was why I thought it deserved a downvote. My sugestion would be to not answer questions you don't understand.

0

Alright. I try not to, which is why I leave a LOT of questions alone, but this one, I just thought I knew. My bad.

0

My ego is very high.

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