How to fix NPC walk animation glitching

I have some NPCs which walk around in my game. But they’re walk animations are glitching.

I’m just using the default animation script that is also used by the Player.
Any ideas on why this is happening?

Hear is the code for my pathfinding

local function moveNPCToWaypoints(npcPart, npcPathFolder)
	local waypoints = npcPathFolder:GetChildren()
	local nextWayPointIndex
	local reachedConnection
	local blockedConnection

	local currentWaypointIndex = 1

	local Walkspeed = npcPart:GetAttribute("Walkspeed")

	local humanoid = npcPart:FindFirstChild("Humanoid")
	humanoid.WalkSpeed = Walkspeed
	npcPart.PrimaryPart:SetNetworkOwner(nil)

	local function getPath(destination)
		local path = Pathfinding:CreatePath({
			AgentHeight = 3;
			AgentRadius = 2.25;
			AgentCanJump = false;
			AgentCanClimb = false;

			Consts = {
				Waiter = 100;
				DangerZoner = math.huge
			}
		})

		if npcPart ~= nil and npcPart:FindFirstChild("HumanoidRootPart") then
			path:ComputeAsync(npcPart.HumanoidRootPart.Position, destination.Position)
		end

		return path
	end

	local function walkTo(destination)
		local path = getPath(destination)

		if path.Status == Enum.PathStatus.Success then
			for index, waypoint in pairs(path:GetWaypoints()) do
				humanoid:MoveTo(waypoint.Position)
				humanoid.MoveToFinished:Wait()
			end
		else
			if npcPart ~= nil and npcPart:FindFirstChild("HumanoidRootPart") then
				humanoid:MoveTo(destination.Position - (npcPart.HumanoidRootPart.CFrame.LookVector * 10))
			end
		end
	end

	local function Patrol()
		local currentWaypoint = waypoints[currentWaypointIndex]
		walkTo(currentWaypoint)

		if currentWaypoint:FindFirstChild("WaitTime") then
			local WaitTimeValue = currentWaypoint.WaitTime.Value
			wait(WaitTimeValue)
		end

		currentWaypointIndex = currentWaypointIndex + 1
		if currentWaypointIndex > #waypoints then
			currentWaypointIndex = 1 -- Reset back to the first waypoint
		end
	end

	while wait(.01) do
		Patrol()
	end
end
2 Likes

the only way i can think of is the the walking animation being too short, but i don’t know. i think a walk animations length (in key frames) is 40, or something around there but i dint remember.

1 Like

How are you moving the NPC or playing the animations? It looks like you’re either constantly playing the animation over and over again (without checking if its already playing) OR you’re pathfinding and the NPC is briefly stopping at each waypoint (causing the animation to briefly stop) before resuming to the next.

1 Like

The weird thing is that it works perfectly in studio but not ingame. I have updated my original post to contain the code, if you’d want to see it