Pathfinding NPC's walk animation is not working properly

For some reason my pathfinding npc’s animation is replaying after 1-2 seconds of playing, anyone knows why that happening?

Could you please share the code that plays animation?

local hum = script.Parent.Humanoid
local animator = hum:WaitForChild("Animator")

local deactivatedvalue = script.Parent:WaitForChild("Deactivated")

-- load animations
local idle = script.Idle
local idlean = animator:LoadAnimation(idle)
idlean.Priority = Enum.AnimationPriority.Idle
idlean.Looped = true

local idle2 = script.Idle2
local idle2an = animator:LoadAnimation(idle2)
idle2an.Priority = Enum.AnimationPriority.Idle
idle2an.Looped = true

local deactivated = script.Deactivated
local deactivatedan = animator:LoadAnimation(deactivated)
deactivatedan.Priority = Enum.AnimationPriority.Idle
deactivatedan.Looped = true

local walk = script.Walk
local walkan = animator:LoadAnimation(walk)
walkan.Priority = Enum.AnimationPriority.Action
walkan.Looped = true

local run = script.Run
local runan = animator:LoadAnimation(run)
runan.Priority = Enum.AnimationPriority.Action
runan.Looped = true

-- start with idle animation
idlean:Play()

-- handle movement animations
hum.Running:Connect(function(speed)
	if speed > 8 then
		-- if running
		if not runan.IsPlaying then
			runan:Play()
			runan:AdjustSpeed(speed / 16) --adjust speed based on actual movement
		end
		walkan:Stop()
		idlean:Stop()
		idle2an:Stop()
		deactivatedan:Stop()
	elseif speed > 0.1 then
		-- if walking
		if not walkan.IsPlaying then
			walkan:Play()
			walkan:AdjustSpeed(speed / 16) --adjust speed based on actual movement
		end
		runan:Stop()
		idlean:Stop()
		idle2an:Stop()
		deactivatedan:Stop()
	else
		if deactivatedvalue.Value == false then
			-- if not moving
			if not idlean.IsPlaying then
				idlean:Play()
			end
			walkan:Stop()
			runan:Stop()
			idle2an:Stop()
			deactivatedan:Stop()
			
		elseif deactivatedvalue.Value == true then
			-- if not moving
			if not deactivatedan.IsPlaying then
				deactivatedan:Play()
			end
			walkan:Stop()
			runan:Stop()
			idlean:Stop()
			idle2an:Stop()
		end
	end
end)

-- jumping
hum.Jumping:Connect(function()
	-- jump anim
end)

-- Handle landing
hum.StateChanged:Connect(function(oldState, newState)
	if newState == Enum.HumanoidStateType.Landed then
		-- reset animations when landing
		if hum.MoveDirection.Magnitude < 0.1 then
			idlean:Play()
		end
	end
end)

tested this with default animations, works for me


I’d recommend moving

out of conditions

because humanoid.Running fires each time when speed changes.

I’d recommend checking if your animations are correct.

I found the solution in this post Humanoid.MoveToFinished:Wait() Makes The Walking Animation Break