Animation script question

Okay so i have this script where the player can press shift to gain speed. they can hold shift to gain speed constantly.
this caps at a walkspeed of 50. if the player lets go, they’ll decelerate until they reach a walkspeed of 16.

now, i have this animation but i want it to play when the player reaches a walkspeed of 30, i don’t know how i would go around doing this as methods i’ve tried tend to have annoying bugs or works poorly.

would anyone know How i can make an animation start playing when the player reaches or ascends the specific walkspeed, and how to make the animation stop when they go below that limit?

i’m not asking for scrpits, i’m asking for ideas. i already have the running script itself made but this part im having trouble with


also this is the main part of the script. it repeats so i can’t play the animation in the repeats or the animation would just repeat itself without playing fully

update: i think co-routines will help me out here, does anyone know how they work? i’ve read the devhub explanation but i still don’t get it…

i figured coroutine out, but my script is having some issues

UIS = game:GetService("UserInputService")
plr = game.Players.LocalPlayer
print(plr)

plr.CharacterAdded:Connect(function(Character)
	local Char = Character
	local Animator = Char:WaitForChild("Humanoid"):FindFirstChild("Animator")
	local Run2 = Instance.new("Animation")
	Run2.AnimationId = "rbxassetid://7422019829"
	Run2.Parent = Char
	local RunTrack2 = Animator:LoadAnimation(Run2)
	RunTrack2.Looped = true
local Running = false
UIS.InputBegan:Connect(function(Key)
		print("Keyboard User")
		Key = Key.KeyCode
		if Key == Enum.KeyCode.LeftShift then
			if Running == false then
				Running = true
			print("Started running")
			end
		end
		coroutine.wrap(function()
			repeat
				Char.Humanoid.WalkSpeed = Char.Humanoid.WalkSpeed + 2
					print(Char.Humanoid.WalkSpeed)
					wait(0.2)
			until Char.Humanoid.WalkSpeed == 50 or not Running
		end)()
		
		if Char.Humanoid.WalkSpeed == 30 or Char.Humanoid.WalkSpeed >= 30 then
			RunTrack2:Play()
		end
		
				UIS.InputEnded:Connect(function(Ekey)
					if Ekey.KeyCode == Enum.KeyCode.LeftShift then
					if Running == true then
						Running = false
				end
				
				coroutine.wrap(function()
					repeat
						Char.Humanoid.WalkSpeed = Char.Humanoid.WalkSpeed - 5
						print(Char.Humanoid.WalkSpeed)
						wait(0.1)
				until Char.Humanoid.WalkSpeed <= 16 or Char.Humanoid.WalkSpeed == 16 or Running
				end)()
				
				if Char.Humanoid.WalkSpeed <= 30 then
					RunTrack2:Stop()
				end
				
				if Char.Humanoid.WalkSpeed <= 16 then 
					Char.Humanoid.WalkSpeed = 16
				end
			end
		end)
	end)
end)

my character accelerates infinitley, and it speeds up and slows down much faster than it should. why is this happening?

So, You’re trying to make the animation work when it’s reaching 30 walkspeed and above?

Yeah, when the player reaches 30 walkspeed or more, the animation plays

when they go below 30, the animation stops

normally this would be simple but i have loops adding the speed up. and as people know, nothing will play outside the loop until the loop ends.

this means if i ran the animation after the loop, the animation wouldn’t play until they reached the maximum speed limit, when it should play long before that.

if i played it in the loop then the animation would play at 30, but it won’t stop playing causing that one effect where the animation just repeats the frames in the given time (im making the loop wait 0.2 seconds so it’ll repeat the first 0.2 seconds of the animation when it should be the whole thing) so i can’t run it inside the loop either

running it before the loop would make it play before the player reaches the required speed as well…

1 Like

Hmm, maybe try putting it in?

ws = game.Players.LocalPlayer.Character.Humanoid.Walkspeed

if ws > 30 then
--RUN ANIMATION
else
--STOP ANIMATION
end
1 Like

I was having the same issue with one of my sprinting systems, the player would just keep on accelerating, breaking the speed cap, and slow down all of a sudden after I let go of the sprinting button. I too dont know why this is happening, and the best fix I can give is to break the loop after the speed cap has been reached, and play the animation.

1 Like

i could do this but i need the player to reach the capped speed limit as something else will be happening when they do so i can’t break it sadly

this won’t work in this instance because;
→ playing it before the loop would make the animation play before the player reaches the specific walkspeed
→ playing it in the loop would repeat the animation
→ playing it after the loop would delay the animation, playing it at the capped limit

1 Like

No worries, just make a variable for the player’s current speed and then do what you want to do with a simple if statement.

like:

local player_CurrSpeed = player.Character.humanoid.WalkSpeed

if player_CurrSpeed == 30 then
     break()
    --do what you wanan do
end

so what you mean is break the loop, do the thing then create the loop again so it can reach the capped limit?
or is 30 becoming the limit?

You can simply wrap the loop in a co-routine and resume it after the speed cap goes under 30, which will start the loop again. you can do this with a simple if statement.

Im pretty sure there is not a function to basically pause the co routine, but coroutine.yield works as well.

read more here: coroutine | Roblox Creator Documentation

1 Like

ill try this, for now i’ll make this as the solution. thank you!

Glad to help. Let me know if there are some errors, ill try my best to help you :slight_smile:

Just add a task.wait() inside the playing loop to allow for the animation to play out.

Humanoid.Running is an event that fires when the speed changes. No need for loops. Just connect through the event. Example code provided.

Humanoid.Running (roblox.com)