How do I have an animation start and stop playing under specific circumstances?
I'm trying to have a sprinting animation that plays when the walk speed is above 16. I could have it so that it plays when the player presses shift and stops when shift is released, but I have a stamina bar that automatically changes the walkspeed back to normal when it depletes, so having it play with left shit wouldn't work. The code that I have successfully plays the animation, but it has issues stopping it. I get an error saying "attempt to index nil with 'Stop'", which sometimes keeps the animation on loop even after I stop moving and other times works perfectly until somewhere down the line the animation loops again. Here's the script:
01 | local character = script.Parent |
02 | local Humanoid = character:WaitForChild( "Humanoid" ) |
04 | local RunService = game:GetService( "RunService" ) |
06 | Humanoid.Running:Connect( function (speed) |
09 | Anim = Instance.new( 'Animation' ) |
11 | PlayAnim = character.Humanoid:LoadAnimation(Anim) |