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

How do I make an animation only play once after touching an object?

Asked by 4 years ago

I am making an obby sort of game and I have a lava jump like obstacle, and I have a script so that if you touch the object, a death animation plays. The issue is that sometimes the animation doesn't play, and sometimes, it plays the 1st few frames then repeats them. I think this is because I have it set the player's walkspeed to 0, and so the game thinks that the character is hitting the object over and over, therefore playing the animation over and over. I thought maybe I could make it so the player moves or gets pushed to another place, but, 1, their walkspeed is 0, and 2, I don't know how to do it. If anyone could help me then it would be very much appreciated! My code is below.

function onTouched(hit)
    local human = hit.Parent:findFirstChild("Humanoid")
    if human then
    human.WalkSpeed = 0
    local anim = human:LoadAnimation(script.Parent.Animation)
    anim:Play()
    wait(2)
    human.Health = 1
    wait(0.5)
    human.Health = 0
    end
end
script.Parent.Touched:connect(onTouched)

Thanks.

0
By the way, I forgot to mention, sometimes the animation plays for less than a second then the character just goes back to the idle. benawesomeness8954 0 — 4y
0
Do hit.Parent.Animate.Disabled = true User#29913 36 — 4y
0
I just tried that but the only thing that would fix would make it so the character doesn't go back to the idle. Sometimes it will work, sometimes it will just play the 1st few frames over and over, and sometimes it will play the animation but then replay it and start playing the 1st few frames even though the animation isn't on loop. benawesomeness8954 0 — 4y
0
So I just figured out that the problem is that the game registers that the player is hitting the object over and over. I put in the script to print whenever it does and it went over 7 times. I don't know how to make it so that it can only happen once. benawesomeness8954 0 — 4y
View all comments (2 more)
0
@benawesomeness8954, when you play the animation set a debounce to true so that in the touch listener only if it's false it runs (You can set it back when you want to) BlackOrange3343 2676 — 4y
0
Wait I figured it out! You were right! Thank you! benawesomeness8954 0 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

BlackOrange3343 told me to set a debounce and I looked up a roblox dev page on it and I tried something and it fixed it! Here is the code.

local anim = false
script.Parent.Touched:connect(function(hit)

    if not anim then

        anim = true
    if hit.Parent then
        human = hit.Parent:FindFirstChild("Humanoid")
        local anim = human:LoadAnimation(script.Parent.Animation)
    anim:Play()
    end
    wait(10)
    anim = false

    end

end)    

Thank you BlackOrange3343.

Ad

Answer this question