Attempt to index nil with 'WaitForChild'

I am getting this error

Attempt to index nil with 'WaitForChild'

Code:

	if script.Parent:WaitForChild("Humanoid").Health == 0 then
--other code
end
3 Likes

Can you show the whole script? The error means that Humanoid doesn’t exist

1 Like

Is this script inside of a character? If not, then you need to change the script.Parent part to whatever hierarchy the character is at.

1 Like

The script works just fine, it just errors.

Oh alright. That is good then :slight_smile:

No, it errors every time it runs

if this is in StarterCharacter scripts, then do this so it won’t do any errors:

if script.Parent then
    if script.Parent:WaitForChild("Humanoid").Health == 0 then
    end
end

It’s in an NPC, not StarterCharacter

Does it have a Humanoid? if not then insert one

Yes, it does. Everything about it works except it outputs this error

Does it have a humanoid in the NPC when you test?

Yes. I tried FindFirstChild and the same error

Do you have any backdoor ruining your game?

I don’t think so (characters.)

How do you add the script to the npc? Is it parented to nil before it gets parented to the npc?

No its a part of it when the game starts

What happens if you put

if not script.Parent then
    script.AncestryChanged:Wait()
end

in the beginning of the script?

while wait() do
	if script.Parent:WaitForChild("Humanoid").Health == 0 then

Nothing different

This error occurs when the instance in which you are waiting for a child is apparently nil. It looks like that the script.Parent is at some point being destroyed, currently doesn’t exists or it never exists.

3 Likes

The reason this error is occurring is NOT BECAUSE THE HUMANOID DOESN’T EXIST. It is because script.Parent is nil. If you wanted to recreate this error, you could do something like this:

local var = nil;
var:WaitForChild(“PogChamp”);

error output: attempt to index nil with ‘WaitForChild’.

so, how do you fix this? as @NilFloat said, the parent is most-likely being destroyed at some point (before the WaitForChild function activates), which is causing your error. I would check other scripts/surrounding code to make sure this isn’t the case. After you do that, you could initialize the humanoid at the top of your code, like this:

local human = script.Parent:WaitForChild(“Humanoid”);

You should really only ever use WaitForChild() once on an instance. Once you know it exists, you can use it however you’d like without having to worry about it not existing (until it is destroyed, of course).

Anyway, I don’t mean to assume what you’re doing in your code, but it seems like you’re constantly checking for if the humanoid’s health reaches zero. There’s actually an event associated with Humanoid that will check when it dies, so you don’t have to constantly loop at check.

Edit:
(error) attempt to index nil with ‘WaitForChild’ = instance calling WaitForChild is nil.
(warning) infinite yield possible on WaitForChild = instance calling WaitForChild does not contain target child :frowning:

7 Likes