Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
Echtic 128
5 years ago

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?

0
you dont need to check if "h" is nil User#23365 30 — 5y

1 answer

Log in to vote
1
Answered by
oilsauce 196
5 years ago
Edited 5 years ago

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).

Ad

Answer this question