Shift to sprint animation keeps going

I am trying to make a shift to sprint script but if I hold the Left shift and stand still it plays the animation anyway. I fixed this using:

(humanoid.MoveDirection.Magnitude > 0)

Now, if I sprint while running and stop moving while holding the shift button, it still plays the animation. How can I fix this? Here is the script:

local UIS = game:GetService('UserInputService')
local Player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait();
local Character = Player.Character
local humanoid = Player.Character:WaitForChild("Humanoid")



UIS.InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift and (humanoid.MoveDirection.Magnitude > 0) then
  Character.Humanoid.WalkSpeed = 34
  local Anim = Instance.new('Animation')
		Anim.AnimationId = 'rbxassetid://6732670706'
  PlayAnim = Character.Humanoid:LoadAnimation(Anim)
  PlayAnim:Play()
 end
end)


UIS.InputEnded:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
  Character.Humanoid.WalkSpeed = 16
  PlayAnim:Stop()
 end
end)
5 Likes

I don’t particularly know why your code is messing up, but Humanoid:LoadAnimation is deprecated according to the API reference. https://developer.roblox.com/en-us/api-reference/function/Humanoid/LoadAnimation

What is a new one I can use? since this one is deprecated?

Some of the APIs aren’t updated, but this one is.

Just use this:

Humanoid.Animator:LoadAnimation(Anim)
1 Like

It worked but now I have the main issue from the post: if I sprint while running and stop moving while holding the shift button, it still plays the animation. How can I fix this?

how about this

humanoid:GetPropertyChangedSignal("WalkSpeed"):Connect(function()
    if humanoid.WalkSpeed == 0 then
        PlayAnim:Stop()
    end
end)

It still doesn’t work. It glitches the animation playing forever.

not sure if its going to work but try:

local UIS = game:GetService('UserInputService')
local Player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait();
local Character = Player.Character or Player.CharacterAdded:Wait()
local humanoid = Player.Character:WaitForChild("Humanoid")
local PlayAnim

if humanoid then
	local animator = humanoid:FindFirstChildOfClass("Animator")
	if animator then
		local Anim = Instance.new('Animation')
		Anim.AnimationId = 'rbxassetid://6732670706'
		PlayAnim = animator:LoadAnimation(Anim)
	end
end


UIS.InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift and (humanoid.MoveDirection.Magnitude > 0) then
		Character.Humanoid.WalkSpeed = 34
		PlayAnim:Play()
	end
end)


UIS.InputEnded:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Character.Humanoid.WalkSpeed = 16
		PlayAnim:Stop()
	end
end)

Nope. When I let go of shift it keeps playing the animation forever.

The problem is that the only thing stopping the Sprint animation from playing is when InputEnded is fired. So of course not letting go of the Shift key would cause the animation to continue playing.

You need to create another function that keeps track of the player’s WalkSpeed (MoveDirection.Magnitude won’t work as it is always either 0 or 1, making it difficult to use when determining if the player is sprinting or walking.)

You should remove the playing of the Sprinting animation from the UserInputService functions altogether and have it performed in a separate Event, such as a listener for the Humanoid’s WalkSpeed.

Try something like…

local sprintAnim = Character.Humanoid.Animator:LoadAnimation(sprintAnimation)
local sprinting = false --To prevent the animation from being played or stopped twice
Character.Humanoid:GetPropertyChangedSignal("WalkSpeed"):Connect(function()
    if Character.Humanoid.WalkSpeed > 16 and sprinting == false then
        sprinting = true
        sprintAnim:Play()
    elseif Character.Humanoid.WalkSpeed <= 16 and sprinting == true then
        sprinting = false
        sprintAnim:Stop()
    end
end)
1 Like

ive updated the code a little bit, if this doesnt work then i dont know how to fix it

local UIS = game:GetService('UserInputService')
local Player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait();
local Character = Player.Character or Player.CharacterAdded:Wait()
local humanoid = Player.Character:WaitForChild("Humanoid")
local PlayAnim

if humanoid then
	local animator = humanoid:FindFirstChildOfClass("Animator")
	if animator then
		local Anim = Instance.new('Animation',animator)
		Anim.AnimationId = 'rbxassetid://6732670706'
		PlayAnim = animator:LoadAnimation(Anim)
	end
end


UIS.InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift and (humanoid.MoveDirection.Magnitude > 0) then
		Character.Humanoid.WalkSpeed = 34
		PlayAnim:Play()
humanoid:GetPropertyChangedSignal("WalkSpeed"):Connect(function()
    if humanoid.WalkSpeed == 0 then
        PlayAnim:Stop()
    end
end)
	end
end)


UIS.InputEnded:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Character.Humanoid.WalkSpeed = 16
		PlayAnim:Stop()
	end
end)
9 Likes