Sprint animation not stopping

Hello, good evening (or morning idk).

I’m making a sprint script, and I thought everything was working fine until I found a weird bug in the animation.

  1. What do you want to achieve?

The idea is that the animation plays only when the player is moving (walking), and later I want to make it so that the animation stops when you jump (I’m too lazy to figure out how to do that yet).

  1. What is the issue?

The issue is that when another animation (Jump, fall, etc.) plays, the running animation will continue even if I’m no longer holding shift.

  1. What solutions have you tried so far?

I tried using while loops that stop the animation when the player is not holding down shift. But I think I might have used the loops the worng way (Or maybe I shouldn’t be using them in the first place)

local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:FindFirstChildOfClass("Humanoid")

local WalkSpeedToReach = 25
local OriginalWalkSpeed = Humanoid.WalkSpeed

local UIS = game:GetService("UserInputService") 

local Walking = script:WaitForChild("Walking") -- Value
local Running = script:WaitForChild("Running")

local RunAnim = Humanoid:LoadAnimation(script.RunAnim)
local WalkAnim = Character.Animate.walk.WalkAnim
local WalkAnimId = WalkAnim.AnimationId

UIS.InputBegan:Connect(function(input)
	
	if input.KeyCode == Enum.KeyCode.LeftShift then -- Detect if shift input started
		
		if script.Walking.Value == true and Character.Humanoid.MoveDirection.Magnitude > 0 then -- See if Walking = true
			
			Humanoid.WalkSpeed = OriginalWalkSpeed + (WalkSpeedToReach - OriginalWalkSpeed) -- Change speed
			
			WalkAnim.AnimationId = script.RunAnim.AnimationId
			Humanoid:LoadAnimation(WalkAnim)
			
			RunAnim:Play()
			RunAnim:AdjustSpeed(1.5)
			
			Running.Value = true
			
		end
		
	end
	
end)

UIS.InputEnded:Connect(function(input)
	
	if input.KeyCode == Enum.KeyCode.LeftShift then -- Detect if shift input ended
		
		Humanoid.WalkSpeed = Humanoid.WalkSpeed - (WalkSpeedToReach - OriginalWalkSpeed) -- Change speed

		RunAnim:Stop()

		WalkAnim.AnimationId = WalkAnimId
		Humanoid:LoadAnimation(WalkAnim)
		
		Running.Value = false
		
	end

end)

while true do
	
	if Character.Humanoid.MoveDirection.Magnitude > 0 then -- Set walking to true or false
		
		Walking.Value = true
		--print("True")
		
	else
		
		Walking.Value = false
		RunAnim:Stop()
		print("Stopped")
		
	end
	
	if Running.Value == false then

		RunAnim:Stop()

		WalkAnim.AnimationId = WalkAnimId
		--Humanoid:LoadAnimation(WalkAnim)

		print("Stopped Running")

	end
	
	wait(.0000000000001)
	
end

image
robloxapp-20220606-1551038.wmv (2.9 MB)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

Alright. So, you should not actually put it in a while loop; since that really doesn’t make sense when you can just use Humanoid.Running property that gets played if the Humanoid is running. Which would mean that, those values that you’ve made to detect if they’re running or walking:

...

local Walking = script:WaitForChild("Walking")
local Running = script:WaitForChild("Running") 

...

should both be removed, because you can just make a local value local Running = false to do this for you.

If they aren’t running, Running = false. Otherwise, Running = true.

How you would use this in your script would to put the value inside of your UIS.InputBegan event. Would look like this:

-- ... We're assuming you've removed the two values above.
local Running = false

UIS.InputBegan:Connect(function(input)
	
	if input.KeyCode == Enum.KeyCode.LeftShift and not Running then -- Detect if shift input started
			
			Humanoid.WalkSpeed = OriginalWalkSpeed + (WalkSpeedToReach - OriginalWalkSpeed) -- Change speed
			
			WalkAnim.AnimationId = script.RunAnim.AnimationId
			Humanoid:LoadAnimation(WalkAnim)
			
			RunAnim:Play()
			RunAnim:AdjustSpeed(1.5)
			
			Running = true
			
		end

end)

UIS.InputEnded:Connect(function(input)
	
	if input.KeyCode == Enum.KeyCode.LeftShift and Running then -- Detect if shift input ended
		
		Humanoid.WalkSpeed = Humanoid.WalkSpeed - (WalkSpeedToReach - OriginalWalkSpeed) -- Change speed

		RunAnim:Stop()

		WalkAnim.AnimationId = WalkAnimId
		Humanoid:LoadAnimation(WalkAnim)
		
		Running = false
		
	end

end)

This above code CAN get optimized WAY better. But since this isn’t the answer you’re looking for, I won’t go deep into that (you could PM me about it though!)

Then now, to the next part of the code would be to replace that while true do statement, and instead have it be the Humanoid.Running event.

It’s pretty simple to do, and what I can tell you is that you have it right with the MoveDirection part of the code.

Humanoid.Running:Connect(function(speed)

	if Humanoid.MoveDirection ~= Vector3.new(0,0,0) and Running and not RunAnim.IsPlaying then
	
		RunAnim:Play()
		RunAnim:AdjustSpeed(1.5)

	elseif Humanoid.MoveDirection == Vector3.new(0,0,0) and Running and RunAnim.IsPlaying then

		RunAnim:Stop()
	elseif HumanoidMoveDirection ~= Vector3.new(0,0,0) and not Running and RunAnim.IsPlaying then
		
		RunAnim:Stop()
	end
end)

-- ... There's actually another code we would have to add down here for stopping the running when a player jumps.

Yeah, we have to also have the part where we stop it when it jumps, like you wanted. So, same process. But Instead of Humanoid.Running, we would have to do Humanoid.Jump. What we would have to do is just simply check if the animation is playing, and of course stop it.

Humanoid.Jumping:Connect(function(active)
	
	if Running and RunAnim.IsPlaying then

		RunAnim:Stop()
	end
end)

If you was to put all of this together, it should look something like this:

local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:FindFirstChildOfClass("Humanoid")

local WalkSpeedToReach = 25
local OriginalWalkSpeed = Humanoid.WalkSpeed

local UIS = game:GetService("UserInputService") 

local Running = false

local RunAnim = Humanoid:LoadAnimation(script.RunAnim)
local WalkAnim = Character.Animate.walk.WalkAnim
local WalkAnimId = WalkAnim.AnimationId

-- Code to detect Input

UIS.InputBegan:Connect(function(input)
	
	if input.KeyCode == Enum.KeyCode.LeftShift and not Running then -- Detect if shift input started
			
			Humanoid.WalkSpeed = OriginalWalkSpeed + (WalkSpeedToReach - OriginalWalkSpeed) -- Change speed
			
			WalkAnim.AnimationId = script.RunAnim.AnimationId
			Humanoid:LoadAnimation(WalkAnim)
			
			RunAnim:Play()
			RunAnim:AdjustSpeed(1.5)
			
			Running = true
			
		end

end)

UIS.InputEnded:Connect(function(input)
	
	if input.KeyCode == Enum.KeyCode.LeftShift and Running then -- Detect if shift input ended
		
		Humanoid.WalkSpeed = Humanoid.WalkSpeed - (WalkSpeedToReach - OriginalWalkSpeed) -- Change speed

		RunAnim:Stop()

		WalkAnim.AnimationId = WalkAnimId
		Humanoid:LoadAnimation(WalkAnim)
		
		Running = false
		
	end

end)

-- Detecting if player is moving while Running = true

Humanoid.Running:Connect(function(speed)

	if Humanoid.MoveDirection ~= Vector3.new(0,0,0) and Running and not RunAnim.IsPlaying then
	
		RunAnim:Play()
		RunAnim:AdjustSpeed(1.5)

	elseif Humanoid.MoveDirection == Vector3.new(0,0,0) and Running and RunAnim.IsPlaying then

		RunAnim:Stop()
	elseif HumanoidMoveDirection ~= Vector3.new(0,0,0) and not Running and RunAnim.IsPlaying then
		
		RunAnim:Stop()
	end
end)

-- Detecting if Player is jumping while Running = true

Humanoid.Jumping:Connect(function(active)
	
	if Running and RunAnim.IsPlaying then

		RunAnim:Stop()
	end
end)

This should work! If there anything regarding to this code or scripting, let me know. And I hope you’re game becomes successful; Happy Deving! <3

  1. Thank you so much for replying, I’ve had this problem for like a year now.
  2. The script seems to work better than the old one, but the weird slow running bug is still there… It happens when you press shift, walk, play another animation while still holding “w” and stop pressing shift.

Could you record the issue for me please? I might know the reason why, but I just want to be sure.

Also, I wonder why people didn’t reply to this, I’ve kind of been viewing this post for a while.

Help.wmv (3.2 MB)

There you go, and again, thank you so much for helping me and sorry for the late answer.

1 Like

Right, so I believe I should’ve also connected some conditions to the InputBegan as well.

UIS.InputBegan:Connect(function(input)
	
	if input.KeyCode == Enum.KeyCode.LeftShift and not Running then -- Detect if shift input started
			
			Humanoid.WalkSpeed = OriginalWalkSpeed + (WalkSpeedToReach - OriginalWalkSpeed) -- Change speed
			
			WalkAnim.AnimationId = script.RunAnim.AnimationId
			Humanoid:LoadAnimation(WalkAnim)
			
			if Humanoid.MoveDirection ~= Vector3.new(0,0,0) then
			
				RunAnim:Play()
				RunAnim:AdjustSpeed(1.5)

			elseif Humanoid.MoveDirection == Vector3.new(0,0,0) then

				-- We won't do anything if they're not moving.
			end

			Running = true
		end

end)

This, may work. Let me know if it does or does not, and also it is no problem; I still am confused on why there wasn’t anyone to help you about this…

That fixed the sprint animation when not moving, but not the other glitch D:
The run animation keeps playing at it’s original speed when the player stops pressing shift after jumping… Sorry for the late answer btw, I was in school.

Im sort of confused here… So when a player jumps after letting go of shift, it doesn’t stop the animation?

It might work if you stop the animation below the elseif statement of detecting if they pressed shift without moving.

Yes, the animation doesn’t stop after you let shift go after jumping.

I tried making it stop, it didn’t work D:

Okay, I might’ve made a mistake here.

Let’s see if this would work:

Humanoid.Jumping:Connect(function(active)
	
	if RunAnim.IsPlaying or Running then

		RunAnim:Stop()
	end
end)

I’ve changed some things here as well:

UIS.InputBegan:Connect(function(input)
	
	if input.KeyCode == Enum.KeyCode.LeftShift and not Running then -- Detect if shift input started
			
			Humanoid.WalkSpeed = OriginalWalkSpeed + (WalkSpeedToReach - OriginalWalkSpeed) -- Change speed
			
			WalkAnim.AnimationId = script.RunAnim.AnimationId
			Humanoid:LoadAnimation(WalkAnim)
			
			if Humanoid.MoveDirection ~= Vector3.new(0,0,0) then
			
				RunAnim:Play()
				RunAnim:AdjustSpeed(1.5)

			elseif Humanoid.MoveDirection == Vector3.new(0,0,0) then

				RunAnim:Stop()
			end

			Running = true
		end

end)

Nope, the animation thing is still there : /

How about this?:

UIS.InputEnded:Connect(function(input)
	
	if input.KeyCode == Enum.KeyCode.LeftShift and RunAnim.IsPlaying and Running then -- Detect if shift input ended
		
		Humanoid.WalkSpeed = Humanoid.WalkSpeed - (WalkSpeedToReach - OriginalWalkSpeed) -- Change speed

		RunAnim:Stop()

		WalkAnim.AnimationId = WalkAnimId
		Humanoid:LoadAnimation(WalkAnim)
		
		Running = false
		
	end

end)

Oh yeah, just to also add, instead of loading the animations into the Humanoid, instead get the animations from the Humanoid’s Animator.

i suggest to change wait(.0000000000001) to wait() so it is gonna wait(0.02) seconds and the script is not stopping becouse it working way to hard i think that is why the script is’nt working proberly here’s the new script:

local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:FindFirstChildOfClass("Humanoid")

local WalkSpeedToReach = 25
local OriginalWalkSpeed = Humanoid.WalkSpeed

local UIS = game:GetService("UserInputService") 

local Walking = script:WaitForChild("Walking") -- Value
local Running = script:WaitForChild("Running")

local RunAnim = Humanoid:LoadAnimation(script.RunAnim)
local WalkAnim = Character.Animate.walk.WalkAnim
local WalkAnimId = WalkAnim.AnimationId

UIS.InputBegan:Connect(function(input)
	
	if input.KeyCode == Enum.KeyCode.LeftShift then -- Detect if shift input started
		
		if script.Walking.Value == true and Character.Humanoid.MoveDirection.Magnitude > 0 then -- See if Walking = true
			
			Humanoid.WalkSpeed = OriginalWalkSpeed + (WalkSpeedToReach - OriginalWalkSpeed) -- Change speed
			
			WalkAnim.AnimationId = script.RunAnim.AnimationId
			Humanoid:LoadAnimation(WalkAnim)
			
			RunAnim:Play()
			RunAnim:AdjustSpeed(1.5)
			
			Running.Value = true
			
		end
		
	end
	
end)

UIS.InputEnded:Connect(function(input)
	
	if input.KeyCode == Enum.KeyCode.LeftShift then -- Detect if shift input ended
		
		Humanoid.WalkSpeed = Humanoid.WalkSpeed - (WalkSpeedToReach - OriginalWalkSpeed) -- Change speed

		RunAnim:Stop()

		WalkAnim.AnimationId = WalkAnimId
		Humanoid:LoadAnimation(WalkAnim)
		
		Running.Value = false
		
	end

end)

while wait() do
	
	if Character.Humanoid.MoveDirection.Magnitude > 0 then -- Set walking to true or false
		
		Walking.Value = true
		--print("True")
		
	else
		
		Walking.Value = false
		RunAnim:Stop()
		print("Stopped")
		
	end
	
	if Running.Value == false then

		RunAnim:Stop()

		WalkAnim.AnimationId = WalkAnimId
		--Humanoid:LoadAnimation(WalkAnim)

		print("Stopped Running")

	end
		
end

also the script is’nt needed to make a animatoin becouse if you cahange the walkspeed the animation as also gonna speed up

The animation bug is still there DD:

local Animator = Humanoid:FindFirstChildOfClass("Animator")

local RunAnimation = script:WaitForChild("RunAnim")
local RunAnim = nil

if Animator then

	RunAnim = Animator:LoadAnimation(RunAnimation)

else
	
	RunAnim = Humanoid:LoadAnimation(RunAnimation)
	
end

Like this, right? I’m not very advanced at scripting (Or I’m just bad at it :P)

Yes; That’s exactly what I want you to do!

This may help fix your issue, if not I’d have to figure something else out. I think it has something to do with the Humanoid.Running.

The bug is still there unfortunately

How about this, instead of changing the walkanim just make a new animation and get that to play. Since, I’m looking at the code and it’s changing the walkanim after running

Wait- So the script works perfectly for you? Did you try jumping while running and then letting go shift?