Determining whether and Npc is running or idle

Hi, I have a script that determines whether the Npc is running or not, when the Npc is running the running animation plays for around 0.5 seconds then restarts.

Code:

		PlayerClone:FindFirstChild("Humanoid").Running:Connect(function(speed)
			if speed == 0 then
				if not RunDebounce then
					RunDebounce = true
					PlayerClone:FindFirstChild("Humanoid"):FindFirstChild("Animator"):LoadAnimation(game.ReplicatedStorage.Animations:FindFirstChild("run")):Stop()
					PlayerClone:FindFirstChild("Humanoid"):FindFirstChild("Animator"):LoadAnimation(game.ReplicatedStorage.Animations:FindFirstChild("GoodAnimsR15"):FindFirstChild("Idle1")):Play()
				end
			elseif speed > 0 then
				if RunDebounce then
					RunDebounce = false
					PlayerClone:FindFirstChild("Humanoid"):FindFirstChild("Animator"):LoadAnimation(game.ReplicatedStorage.Animations:FindFirstChild("run")):Play()
					PlayerClone:FindFirstChild("Humanoid"):FindFirstChild("Animator"):LoadAnimation(game.ReplicatedStorage.Animations:FindFirstChild("GoodAnimsR15"):FindFirstChild("Idle1")):Stop()
				end
			end
		end)

you could do humanoid.Changed then check if the Humanoids MoveDirection is not equal to 0,0,0

it would probably look like this

PlayerClone:FindFirstChild("Humanoid").Changed:Connect:function()
if PlayerClone:FindFirstChild("Humanoid").MoveDirection ~= Vector3.new(0,0,0) then
--Idle
else
--Moving
end
end)

I don’t have studio open so I can’t test it rn

Also just a good habit, you can make animations Variables. this helps save memory because you don’t have to load the animation constantly and also, you can load animations Directly into the humanoids. So the final thing would look like this

RunAnim = PlayerClone:FindFirstChild("Humanoid"):LoadAnimation(game.ReplicatedStorage.Animations:FindFirstChild("run"))


RunAnim:Play()

Thanks for the help about making them variables, but the solution you gave is not really the problem, the code works but the animation restarts

oh. well theres no real way of “Pausing” an Animation so what I do set the animation speed to 0 using Adjust speed, like this
RunAnim:AdjustSpeed(0)

and when your ready to unpause:

RunAnim:AdjustSpeed(1)

Keep in mind, in 90% sure this only works while the Animation is actually playing.