Attempt to index nil with 'FindFirstChild'

game.Players.PlayerAdded:Connect(function(plr)
local humanoid = plr.Character:FindFirstChild("Humanoid")
end)

I have this code and it keeps outputting:
Screen Shot 2020-08-15 at 11.54.33 AM

Does anyone know why?

2 Likes

Use :WaitForChild() instead, as sometimes :FindFirstChild() will fail.

Screen Shot 2020-08-15 at 12.00.52 PM

The player’s character does not exist right as the player joins, so you will have to wait for it, then you can use :FindFirstChild(“Humanoid”) on it

This means the character has not loaded yet so the script does not recognise it as being there.

You can fix this by running the code with the character has been fully loaded by doing the following:

game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(char)
        local humanoid = plr.Character:FindFirstChild("Humanoid")
    end)
end)

This will run as soon as the character is fully added, instead of having to manually add a wait() when you can’t determine exactly how long the character will take to load.

That’s a really bad approach. Please don’t rely on it being three seconds. When they load in in less time, they’ll have to wait for your script to wait that arbitrary number. What if they reset when they spawn in, breaking it? On the other hand, when they take longer to load in, your script straight up breaks. Do an event based approach like Legend suggested.

2 Likes