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.

6 comments
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.
Do hit.Parent.Animate.Disabled = true
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.
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, 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)
Wait I figured it out! You were right! Thank you!