Pathfinding walking animation jittering

Coming to the dev forum since i really need help.

Whats my issue?
My npcs walk jittery with their pathfinding script, which is a common problem when it comes to pathfinding and moveto()

Ive tried multiple things, adding debounces, waits, humanoid.MoveToFinished:Connect(function() then wrapping a wait in that. Ive tried distance based scripts to detect when they have reached the point and add a wait in. Ive tried everything, and nothing is working at all. When i do use the humanoid.MoveToFinished:Connect(function() it completely breaks my entire pathfinding script, the npcs just teleport to the camera parts and then don’t walk in the right way after.

I would really appreciate some help as ive been trying for ages and they still walk all jittery, the solutions i usually use won’t work and end up breaking other things in the script.

here is my script:

local PathfindingService = game:GetService("PathfindingService")

local npc = script.Parent.Parent
local humanoid = npc:WaitForChild("Humanoid")
local rootPart = npc:WaitForChild("HumanoidRootPart")

rootPart:SetNetworkOwner(nil)


local function moveThrough(positions)
	for _, pos in ipairs(positions) do
		local path = PathfindingService:CreatePath({
			AgentRadius = 1,
			AgentHeight = 3,
			AgentCanJump = false,
			AgentCanClimb = false
		})

		path:ComputeAsync(rootPart.Position, pos)

		local waypoints = path.Status == Enum.PathStatus.Success
			and path:GetWaypoints()
			or { { Position = pos, Action = Enum.PathWaypointAction.Walk } }

		for _, wp in ipairs(waypoints) do
			humanoid:MoveTo(wp.Position)
			humanoid.MoveToFinished:Wait()
			wait()
		end
	end
end

local function getIndexedPositions(folderName, tagPrefix, count)
	local folder = workspace.GameAssets.Waypaths:WaitForChild(folderName)
	local points = {}

	for i = 1, count do
		local part = folder:FindFirstChild(tagPrefix .. i)
		if part and part:IsA("BasePart") then
			table.insert(points, part.Position)
		end
	end

	return points
end

function startNPCMovement()
	-- GameHall walk
	moveThrough(getIndexedPositions("GameHall", "Waypoint", 5))

	-- Random camera walk
	local camId = math.random(1, 3)
	local camTarget = workspace.GameAssets:FindFirstChild("CameraBoxNPC" .. camId)
	if camTarget then
		moveThrough({ camTarget:FindFirstChild("CamLine").Position })

		-- Teleport sequence
		rootPart.CFrame = CFrame.new(camTarget:FindFirstChild("TeleportStart").Position)
		camTarget.MainPart.SMILE:Play()
		task.wait(3)
		rootPart.CFrame = CFrame.new(camTarget:FindFirstChild("TeleportEnd").Position)
		camTarget.MainPart.CameraFlash:Play()
	end

	-- RLGL walk
	moveThrough(getIndexedPositions("RLGL", "Diode", 17))
end

game.ReplicatedStorage.Remotes.AIGamehall.OnServerEvent:Connect(function()
	wait(math.random(1,60))
	startNPCMovement()
end)

if anyone can help then i greatly appreciate it.

Use something that isn’t humanoid.MoveToFinished. Use a loop calculating the distance every frame and if the distance reaches or goes under a certain threshold the loop ends, and it continues to the next waypoint.

already tried this one sorry.
it didn’t work either and ended up breaking the script.

Try using SetNetworkOwner of the humanoidrootpart to nil, there was a video on Pathfinding, and they mentioned Jittering, the link is here: https://www.youtube.com/watch?v=MQGopkmsfwQ Try skipping to 5 minutes seconds for the explanation. By the way I believe its due to the server and client fighting over who has ownership, thus causing a “jitter”.

Hope it helps. :smiley:

1 Like