I am getting this error
Attempt to index nil with 'WaitForChild'
Code:
if script.Parent:WaitForChild("Humanoid").Health == 0 then
--other code
end
I am getting this error
Attempt to index nil with 'WaitForChild'
Code:
if script.Parent:WaitForChild("Humanoid").Health == 0 then
--other code
end
Can you show the whole script? The error means that Humanoid doesnât exist
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.
The script works just fine, it just errors.
Oh alright. That is good then ![]()
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.
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 ![]()