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

How can I fix "Attempt to index nil with 'Health'"?

Hi, this is my script:

local wa = script.Parent
while true do
	wait(0.01)
	if wa:FindFirstChild("Humanoid").Health == 0 then
		wa:Destroy()
	end
	wait(0.01)
end

Everything went well but on the Output, it saids "(Where my script is):Attempt to index nil with 'Health'", I had tried searching on how to fix the problem, some of them have similar problem to me so I tried to fix it but there is still an error. Although, my script doesn't have any red underlines there is an error.

1 answer

✓ Accepted answer

It might be because you didn't check if the humanoid exists and that's why it's nil, you can put an if to check if it exists then check the health,like so:

local wa = script.Parent

while true do
	wait()
	if wa:FindFirstChild("Humanoid") then
		if wa:FindFirstChild("Humanoid").Health == 0 then
			wa:Destroy()
		end
	else
		-- This WILL Probably give you an infinite yield, it's optional, kinda
		local humanoid = wa:WaitForChild("Humanoid")
		-- Your code
	end
end

1 comment

0

Thank you so much!

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