How to fix sprint animations

I am trying to make my shift to sprint script do the following:

  • Not override jump animation

  • Not work while not moving

And I have been having trouble trying to achieve this. Here’s my code below:

local UIS = game:GetService('UserInputService')
local Player = game.Players.LocalPlayer
local Character = Player.Character


UIS.InputBegan:connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
	if Character.Humanoid.MoveDirection.Magnitude > 0 then
  Character.Humanoid.WalkSpeed = 24
  local Anim = Instance.new('Animation')
   Anim.AnimationId = 'rbxassetid://8316922787'
  PlayAnim = Character.Humanoid:LoadAnimation(Anim)
	PlayAnim:Play()
	elseif Character.Humanoid.MoveDirection.Magnitude <= 0 then
	PlayAnim:Stop()
  end
 end
end)


UIS.InputEnded:connect(function(input)
 if input.KeyCode == Enum.KeyCode.LeftShift then
  Character.Humanoid.WalkSpeed = 16
  PlayAnim:Stop()
 end
end)
2 Likes
local YourAnimation = "http://www.roblox.com/asset/?id=252557606" -- Your run anim
local ShiftSpeed = 30 -- Speed will be added 
local OriginalSpeed = 16 -- Original speed

local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer

repeat
	wait()
until
Player.Character

local Character = Player.Character

local RunAnimation = Character:WaitForChild("Animate").run:GetChildren()[1]

local OrginalAnimation = Instance.new("StringValue", Character)
OrginalAnimation.Value = RunAnimation.AnimationId

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		RunAnimation.AnimationId = YourAnimation
		Character:WaitForChild("Humanoid").WalkSpeed = ShiftSpeed
	end
end)

UIS.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		RunAnimation.AnimationId = OrginalAnimation.Value
		Character:WaitForChild("Humanoid").WalkSpeed = OriginalSpeed
	end
end)

The sprinting works, but it doesn’t play the animation.

Did you set the animation priority to action?

If you didn’t, do that and re-publish the animation and it should work.

1 Like

I republished the animation (with the animation priority still set to action) and it didn’t work.
I have the script set as a LocalScript in StarterGUI since that was where my last script was.

I can’t find anything wrong with the code. Are there any errors in the output when you try to sprint?

Nope, none at all. I don’t see anything that shows up as an error

I don’t know then ¯_(ツ)_/¯
Nothing is wrong with the code that I see.

Here’s my original script:
https://gyazo.com/b03b236dd7dd2888aa1af92d12b5a135

Here’s the updated one by @puofz :
https://gyazo.com/3e2b21b08f54c5fe8e6c72e3dc2c09ab

As you see the sprinting works but the animation just does not for some reason.

When I try your code it gives an error because you got the player’s character too early. You had to do Player.CharacterAdded:Wait() and then get the player’s character.

Try this:

local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer

Player.CharacterAdded:Wait()

local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local Jumping = false

local Animation = Instance.new("Animation")
Animation.AnimationId = "" -- Insert The Sprint Animation Here. Your Animation Id doesn't seem to be working.
local Animation = Humanoid:LoadAnimation(Animation)	

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift and not Jumping then
		Animation:Play()
		Humanoid.WalkSpeed = 32
	end
end)

UIS.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift and not Jumping then
		Animation:Stop()
		Humanoid.WalkSpeed = 16
	end
end)

Humanoid.Jumping:Connect(function(boolean)
	Jumping = boolean
end)

That code does not work for me at all. I don’t even get an error message but I do not even get faster when I try to sprint.

Make a new template in Roblox Studio and add the code into a local script inside the Starter Pack folder. There might be a script interfering with the code in your game.

Edit: The code will only work if you put an animation id between the strings next to the Animation.AnimationId variable.

Well I put it inside a new game and I realized it didn’t work with StarterGui, I put the script in a new template, and then I put it in my original (obviously in StarterPlayerScripts), they both had the animations working but it still has the same problems I was trying to solve.

  • Not override jump animation
  • Not work while not moving
local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer

Player.CharacterAdded:Wait()

local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")

local Animation = Instance.new("Animation")
Animation.AnimationId = "" -- Insert The Sprint Animation Here. Your Animation Id doesn't seem to be working.
local Animation = Humanoid:LoadAnimation(Animation)	

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Animation:Play()
		Humanoid.WalkSpeed = 32
	end
end)

UIS.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Animation:Stop()
		Humanoid.WalkSpeed = 16
	end
end)

Humanoid.Jumping:Connect(function(jumping)
	if jumping and Animation.IsPlaying then
		Animation:Stop()
	end
end)

Humanoid.Running:Connect(function(speed)
	if speed == 0 and Animation.IsPlaying then
		Animation:Stop()
	end
end)
1 Like

It worked! Thank you so much, the only thing I need to fix is that after you jump the sprinting still works, but the animation stops, I can fix it though. Thank you all for your help!

You’re welcome! If you need any more help let me know

oh mb ye values does not works enough for saving sorry for lazy scripting

local YourAnimation = "http://www.roblox.com/asset/?id=656118852" -- This is ninja run anim pack change it to your run anim just for check
local ShiftSpeed = 30 -- Speed will be added 
local OriginalSpeed = 16 -- Original speed

local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer

repeat
	wait()
until
Player.Character

local Character = Player.Character

local RunAnimation = Character:WaitForChild("Animate").run:GetChildren()[1]
local Humanoid = Character:WaitForChild("Humanoid")
local OrginalAnimation = Humanoid:GetAppliedDescription().RunAnimation

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		RunAnimation.AnimationId = YourAnimation
		Character:WaitForChild("Humanoid").WalkSpeed = ShiftSpeed
	end
end)

UIS.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		RunAnimation.AnimationId = "http://www.roblox.com/asset/?id="..OrginalAnimation
		Character:WaitForChild("Humanoid").WalkSpeed = OriginalSpeed
	end
end)

It’s alright, thanks for your help! It still helped me learn