Run animation playing when not moving

I made a run script that works properly, only issue is that the animation still plays when i am not moving and holding shift. Checked tons of posts and couldnt find anything that would work, any ideas on how to fix this?
heres a clip of the issue
https://gyazo.com/89251340566ae618ec707b36f11718a3
here is the code

UIS.InputBegan:Connect(function(input,gameprocessed)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		
		sprinting = true
		playSprint:Play()
		hum.WalkSpeed = 30
		print("currently Sprinting")
	end
end)

UIS.InputEnded:Connect(function(input,gameprocessed)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		sprinting = false
		playSprint:Stop()
		hum.WalkSpeed = 15
		print("ended Sprinting")
	end
end)
3 Likes

Let’s optimize the script.
We’re going to use RunService for this for best effect.

RunService.Heartbeat is good to check variables / conditions every passing frame.

Remove the playSprint:Play() and playSprint:Stop from both events - we’re going to use the sprinting boolean for this.

And, we’re also going to check if the Humanoid is moving by using

if Humanoid.MoveDirection.Magnitude > 0.1 then
-- humanoid is moving
end

For better effect, we also want to check if the Humanoid isn’t sitting, nor is in the air

if Humanoid.Sit == false and Humanoid.FloorMaterial ~= Enum.Material.Air then -- ~= is the opposite of ==
-- humanoid is not sitting and isnt in the air
end

Here’s how to implement it:

local RunService = game:GetService("RunService")
RunService.Heartbeat:Connect(function()
	if sprinting == true and Humanoid.MoveDirection.Magnitude > 0.1 and Humanoid.Sit == false and Humanoid.FloorMaterial ~= Enum.Material.Air then
		playSprint:Play()
	else
		playSprint:Stop()
	end
end)

The Final Script
It checks if the Sprinting is ready, and if the Humanoid is Moving, is Not Sitting, and Not in the Air - and will play the animation accordingly, otherwise - it will stop the animation.

UIS.InputBegan:Connect(function(input,gameprocessed)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		
		sprinting = true
		hum.WalkSpeed = 30
		print("currently Sprinting")
	end
end)

UIS.InputEnded:Connect(function(input,gameprocessed)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		sprinting = false
		hum.WalkSpeed = 15
		print("ended Sprinting")
	end
end)

local RunService = game:GetService("RunService")
RunService.Heartbeat:Connect(function()
	if sprinting == true and Humanoid.MoveDirection.Magnitude > 0.1 and Humanoid.Sit == false and Humanoid.FloorMaterial ~= Enum.Material.Air then
		playSprint:Play()
	else
		playSprint:Stop()
	end
end)

Try it, let me know!

This works, but now it’s doing this;
https://gyazo.com/feb918cebd792c4d5f2f5e61431493a2
I’m guessing that it keeps on playing the animation over and over again, I’m not too familiar with debounce but maybe that’s a way to fix it? If you know of anything better I also wouldn’t mind using that

OH! Sorry!
Simple!

Replace playSprint:Play() with

if not playSprint.IsPlaying then
	playSprint:Play()
end

Similarly, replace playSprint:Stop() with

if playSprint.IsPlaying then
	playSprint:Stop()
end

The Final Script

UIS.InputBegan:Connect(function(input,gameprocessed)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		
		sprinting = true
		hum.WalkSpeed = 30
		print("currently Sprinting")
	end
end)

UIS.InputEnded:Connect(function(input,gameprocessed)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		sprinting = false
		hum.WalkSpeed = 15
		print("ended Sprinting")
	end
end)

local RunService = game:GetService("RunService")
RunService.Heartbeat:Connect(function()
	if sprinting == true and Humanoid.MoveDirection.Magnitude > 0.1 and Humanoid.Sit == false and Humanoid.FloorMaterial ~= Enum.Material.Air then
		if not playSprint.IsPlaying then
			playSprint:Play()
		end
	else
		if playSprint.IsPlaying then
			playSprint:Stop()
		end
	end
end)

In order to play the animation without spamming, it checks if the Animation Is Not Playing (if it isn’t playing, it will not play at all.

When the sprinting ends (or other conditions aren’t met), it checks if the Animation Is Playing in order to stop it!

6 Likes

Life saver, man. Thanks for the help!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.