Running animation playing when staying still

For my sprint script, the animation plays when the player is standing still. I am aware this is not a bug, but what would I need to do to make this not happen? Any help is appreciated!

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 anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://9580521133"
local animplay = animator:LoadAnimation(anim)


player.CharacterAdded:Connect(function(NewCharacter)
	character = NewCharacter
	humanoid = character:WaitForChild("Humanoid")
	animator = humanoid:WaitForChild("Animator")

	
	anim = Instance.new("Animation")
	anim.AnimationId = "rbxassetid://9580521133"
	animplay = animator:LoadAnimation(anim)
end) 

local function beginSprint(input, gameProcessed)
	if not gameProcessed then
			if character.Attacking.Value == 0 then
			if player.Character.Stun.Value == 0 then
			if input.UserInputType == Enum.UserInputType.Keyboard then
				local keycode = input.KeyCode
					if keycode == Enum.KeyCode.LeftControl then
					 character.Run.Value = 1
						humanoid.WalkSpeed = 32
					animplay:Play()
				end
			end
		end
	end
	end
	end

	local function endSprint(input, gameProcessed)
	if not gameProcessed then
		if player.Character.Attacking.Value == 0 then
			if player.Character.Stun.Value == 0 then
			if input.UserInputType == Enum.UserInputType.Keyboard then
				local keycode = input.KeyCode
				if keycode == Enum.KeyCode.LeftControl then
						humanoid.WalkSpeed = 16
						character.Run.Value = 0
					animplay:Stop()
				end
			end
				end
			end	
	end
	end

userInput.InputBegan:Connect(beginSprint)
userInput.InputEnded:Connect(endSprint)
1 Like
Humanoid.Running:Connect(function(speed)
if speed >= 0.01 then
--(if running, play running animation)
    end
end)

This is done for you in the animate script for regular walking, so as long as your run animation has a higher priority than movement, it should work.

This works, but only at the start, if the player runs and then stops moving, the animation remains playing.The animation also overlaps the walking animation. (If i recall correctly, the running animation has higher priority)

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 anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://9580521133"
local animplay = animator:LoadAnimation(anim)


player.CharacterAdded:Connect(function(NewCharacter)
	character = NewCharacter
	humanoid = character:WaitForChild("Humanoid")
	animator = humanoid:WaitForChild("Animator")

	
	anim = Instance.new("Animation")
	anim.AnimationId = "rbxassetid://9580521133"
	animplay = animator:LoadAnimation(anim)
end) 

local KeyCode = Enum.KeyCode.LeftControl
local function beginSprint(input, gameProcessed)
	if character.Attacking.Value == 0 then
		if player.Character.Stun.Value == 0 then
			if input.KeyCode == KeyCode and not gameProcessed then

				character.Run.Value = 1
				humanoid.WalkSpeed = 32
				animplay:Play()
				
				repeat RunService.Heartbeat:Wait()
					if humanoid.MoveDirection.Magnitude > 0 then
						if not animplay.IsPlaying then
							animplay:Play()
						end
					elseif aimplay.IsPlaying then
						animplay:Stop()
					end
				until not userInput:IsKeyDown(KeyCode)

				character.Run.Value = 0
				humanoid.WalkSpeed = 16
				animplay:Stop()
				
			end
		end
	end
end

userInput.InputBegan:Connect(beginSprint)
2 Likes

Thanks, works perfeclty. Ill look up RunService to try and understand what it is! Have a nice day/night.

1 Like