Sprint Animation Help

Hi!

I’m fairly new to scripting - I mainly build & design graphics, however I decided to try and enter the world of Lua.

https://gyazo.com/f88fb3c906794f629337a4a07cb2b54c

The issue I’m having is the sprint animation being played when the player isn’t moving. There’s probably a really easy fix for this - but I thought I’d ask here just in case.

I’m a little reluctant to share the animation ID, but it may help solve the issue - so it’s included.

Here’s my code:

local UIS = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
local char = plr.Character

UIS.InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		char.Humanoid.WalkSpeed = 20
		local Anim = Instance.new("Animation")
			Anim.AnimationId = 'rbxassetid://9049017304'
			PlayAnim = char.Humanoid:LoadAnimation(Anim)
			PlayAnim:Play()
	end
end)

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

Any help would be greatly appreciated. Thanks!

local UIS = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
local char = plr.Character
local Anim = Instance.new("Animation")
Anim.AnimationId = 'rbxassetid://9049017304'
PlayAnim = char.Humanoid:LoadAnimation(Anim)

UIS.InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		char.Humanoid.WalkSpeed = 20
        if char.Humanoid.MoveDirection ~= Vector3.new(0,0,0) then
		PlayAnim:Play()
        end
	end
end)

UIS.InputEnded:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		char.Humanoid.WalkSpeed = 16
		PlayAnim:Stop()
	end
end)
1 Like
local userInput = game:GetService("UserInputService")
local players = game:GetService("Players")
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://742638842"
animation.Parent = script
local track = animator:LoadAnimation(animation)

userInput.InputBegan:Connect(function(input, _)	
	if input.KeyCode == Enum.KeyCode.LeftShift then
		humanoid.WalkSpeed = 20
		if not track.IsPlaying then
			track:Play()
		end
	end
end)

userInput.InputEnded:Connect(function(input, _)	
	if input.KeyCode == Enum.KeyCode.LeftShift then
		humanoid.WalkSpeed = 16
		track:Stop()
	end
end)

humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
	if humanoid.MoveDirection.Magnitude <= 0 then
		track:Stop()
	else
		if userInput:IsKeyDown(Enum.KeyCode.LeftShift) then
			if not track.IsPlaying then
				track:Play()
			end
		end
	end
end)

This is working well for me, I used Roblox’s “Cartoony Run” animation to test.

1 Like