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

The animation part of the script doesn't work, it says that h is a nil value, can anybody help?

Here's the script:

wait()

function onTouched(part)

	local h = part.Parent:FindFirstChild("Humanoid")
	
	local anim = h:LoadAnimation(script.anim)

	if h~=nil then
		wait(.05)
		h:TakeDamage(5)
		
		anim:Play()
		
		
	end

	wait(1)
end



script.Parent.Touched:connect(onTouched)

The animation part doesn't work but without it the script works perfectly, have i done anything wrong?

1 comment

0

you dont need to check if "h" is nil

1 answer

✓ Accepted answer

Here's a joke:

The patient says: "It hurts when I raise my arm like this." Then the doctor says: "Then don't raise your arm like that".

If it says h is a nil value, it's a nil value. Apparently, at line 5 part.Parent:FindFirstChild("Humanoid") is being ran before the Humanoid exists. Try using WaitForChild() instead of FindFirstChild() so it'll wait until the object is there.

But if you still want to use FindFirstChild(), you can run h:LoadAnimation() AFTER you check if the h is not nil (if h~=nil then).

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