Run Animation playing while the player is idle

Hello whoever is reading this,
I need some help with making my run animation stop when the player is idle as well as having the default jump animation play when in the air instead of the run animation. I have tried using humanoid.MoveDirection.Magnitude > 0 to see if the player was idle but I couldn’t manage to get it to work.

Details:

  • Running Animation Priority is “Movement”

  • The animation is looped

  • Game is set to r6

  • Camera Mode is set to “LockFirstPerson”

Code is below (This is the code before I started trying to fix the issue.)

local uis = game:GetService("UserInputService")
local ts = game:GetService("TweenService")

local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")

local key = Enum.KeyCode.LeftShift
local cam = game.Workspace.CurrentCamera
local addSpeed = 16

local tweeninfoSprint = TweenInfo.new(.3, Enum.EasingStyle.Sine, Enum.EasingDirection.In)
local tweeninfoNotSprint = TweenInfo.new(.3, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)

local tweenSprint = ts:Create(cam, tweeninfoSprint, {FieldOfView = 80})
local tweenNotSprint = ts:Create(cam, tweeninfoNotSprint, {FieldOfView = 70})

local animator = humanoid:WaitForChild("Animator")

local sprintAnimation = Instance.new("Animation")
sprintAnimation.AnimationId =  "rbxassetid://14868306779"

local sprintTrack = animator:LoadAnimation(sprintAnimation)

uis.InputBegan:Connect(function(input, gameProcessed)
	if not gameProcessed then
		if input.KeyCode == key then
			humanoid.WalkSpeed += addSpeed
			sprintTrack:Play()
			tweenSprint:Play()
		end
	end
end)

uis.InputEnded:Connect(function(input, gameProcessed)
	if not gameProcessed then
		if input.KeyCode == key then
			humanoid.WalkSpeed -= addSpeed
			sprintTrack:Stop()
			tweenNotSprint:Play()
		end
	end
end)

Any Help is Appreciated! Thanks in advance.

2 Likes

Before you play the animation you could check the humanoids state type.

local uis = game:GetService("UserInputService")
local ts = game:GetService("TweenService")

local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")

local key = Enum.KeyCode.LeftShift
local cam = game.Workspace.CurrentCamera
local addSpeed = 16

local tweeninfoSprint = TweenInfo.new(.3, Enum.EasingStyle.Sine, Enum.EasingDirection.In)
local tweeninfoNotSprint = TweenInfo.new(.3, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)

local tweenSprint = ts:Create(cam, tweeninfoSprint, {FieldOfView = 80})
local tweenNotSprint = ts:Create(cam, tweeninfoNotSprint, {FieldOfView = 70})

local animator = humanoid:WaitForChild("Animator")

local sprintAnimation = Instance.new("Animation")
sprintAnimation.AnimationId =  "rbxassetid://14868306779"

local sprintTrack = animator:LoadAnimation(sprintAnimation)

uis.InputBegan:Connect(function(input, gameProcessed)
	if not gameProcessed then
		if input.KeyCode == key then
if humanoid:GetState() ~= Enum.HumanoidStateType.Idle then
			humanoid.WalkSpeed += addSpeed
			sprintTrack:Play()
			tweenSprint:Play()
end
		end
	end
end)

uis.InputEnded:Connect(function(input, gameProcessed)
	if not gameProcessed then
		if input.KeyCode == key then
			humanoid.WalkSpeed -= addSpeed
			sprintTrack:Stop()
			tweenNotSprint:Play()
		end
	end
end)

I hope i helped.

1 Like

It says “Idle is not a valid member of Enum.HumanoidStateType”

1 Like

Right, I forgot that’s not a state type.

I know you’ve tried this but maybe you’ve put it in the wrong place?

local uis = game:GetService("UserInputService")
local ts = game:GetService("TweenService")

local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")

local key = Enum.KeyCode.LeftShift
local cam = game.Workspace.CurrentCamera
local addSpeed = 16

local tweeninfoSprint = TweenInfo.new(.3, Enum.EasingStyle.Sine, Enum.EasingDirection.In)
local tweeninfoNotSprint = TweenInfo.new(.3, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)

local tweenSprint = ts:Create(cam, tweeninfoSprint, {FieldOfView = 80})
local tweenNotSprint = ts:Create(cam, tweeninfoNotSprint, {FieldOfView = 70})

local animator = humanoid:WaitForChild("Animator")

local sprintAnimation = Instance.new("Animation")
sprintAnimation.AnimationId =  "rbxassetid://14868306779"

local sprintTrack = animator:LoadAnimation(sprintAnimation)

uis.InputBegan:Connect(function(input, gameProcessed)
	if not gameProcessed then
		if input.KeyCode == key then
if humanoid.MoveDirection == Vector3.new(0,0,0) then
			humanoid.WalkSpeed += addSpeed
			sprintTrack:Play()
			tweenSprint:Play()
end
		end
	end
end)

uis.InputEnded:Connect(function(input, gameProcessed)
	if not gameProcessed then
		if input.KeyCode == key then
			humanoid.WalkSpeed -= addSpeed
			sprintTrack:Stop()
			tweenNotSprint:Play()
		end
	end
end)
1 Like

If that doesn’t work try this.

I’ve also found a post covering how to check if the player is idle or not:

local uis = game:GetService("UserInputService")
local ts = game:GetService("TweenService")

local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")

local key = Enum.KeyCode.LeftShift
local cam = game.Workspace.CurrentCamera
local addSpeed = 16

local tweeninfoSprint = TweenInfo.new(.3, Enum.EasingStyle.Sine, Enum.EasingDirection.In)
local tweeninfoNotSprint = TweenInfo.new(.3, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)

local tweenSprint = ts:Create(cam, tweeninfoSprint, {FieldOfView = 80})
local tweenNotSprint = ts:Create(cam, tweeninfoNotSprint, {FieldOfView = 70})

local animator = humanoid:WaitForChild("Animator")

local sprintAnimation = Instance.new("Animation")
sprintAnimation.AnimationId =  "rbxassetid://14868306779"

local sprintTrack = animator:LoadAnimation(sprintAnimation)

uis.InputBegan:Connect(function(input,typing)
	if input.KeyCode == key and not typing then
		repeat
			if humanoid.MoveDirection ==  Vector3.new(0,0,0) then
							sprintTrack:Play()
			tweenSprint:Play()
				humanoid.WalkSpeed += addSpeed
			else
				sprintTrack:Stop()
				tweenNotSprint:Play()
			end
		until uis.InputEnded
	end
end)

uis.InputEnded:Connect(function(input, gameProcessed)
	if not gameProcessed then
		if input.KeyCode == key then
			humanoid.WalkSpeed -= addSpeed
			sprintTrack:Stop()
			tweenNotSprint:Play()
		end
	end
end)```

the script doesn’t work and still allows the run anim to play. Its something to do with holding the shift key, if there was a way to force end the input if the player wasn’t moving it might work.

2 Likes

I found a free script on the toolbox to prevent the bugs from happening. thanks for trying to help me!

1 Like

Can you send me link of this script? I have exact same problem as you had and cant fix it still

1 Like

compare your script to the one you found so you learn why it happened and don’t make the same mistake again

1 Like

local Player = game.Players.LocalPlayer
local Character = workspace:WaitForChild(Player.Name)
local Humanoid = Character:WaitForChild(‘Humanoid’)

local RunAnimation = Instance.new(‘Animation’)
RunAnimation.AnimationId = ‘rbxassetid://1721906652’
RAnimation = Humanoid:LoadAnimation(RunAnimation)

Running = false

function Handler(BindName, InputState)
if InputState == Enum.UserInputState.Begin and BindName == ‘RunBind’ then
Running = true
Humanoid.WalkSpeed = 50
elseif InputState == Enum.UserInputState.End and BindName == ‘RunBind’ then
Running = false
if RAnimation.IsPlaying then
RAnimation:Stop()
end
Humanoid.WalkSpeed = 16
end
end

Humanoid.Running:connect(function(Speed)
if Speed >= 10 and Running and not RAnimation.IsPlaying then
RAnimation:Play()
Humanoid.WalkSpeed = 50
elseif Speed >= 10 and not Running and RAnimation.IsPlaying then
RAnimation:Stop()
Humanoid.WalkSpeed = 16
elseif Speed < 10 and RAnimation.IsPlaying then
RAnimation:Stop()
Humanoid.WalkSpeed = 16
end
end)

Humanoid.Changed:connect(function()
if Humanoid.Jump and RAnimation.IsPlaying then
RAnimation:Stop()
end
end)

game:GetService(‘ContextActionService’):BindAction(‘RunBind’, Handler, true, Enum.KeyCode.LeftShift)

2 Likes