MoveToFinished:Wait() causing stutter

Hey everyone!

I’m using a single script to control multiple enemies. The pathfinding MoveToFinished plays the idle animation when calculating the next waypoint position for the zombie. It causes a slight stutter in the animation. Is there any workarounds for this?

I’ve tried different approaches (i.e, calculating the magnitude) but they lose the pathfinding and easily get confused/stuck.

Any help or suggestions are greatly appreciated!

Best, Water

function pathToTarget(zombie)
	local path = game:GetService("PathfindingService"):CreatePath()
	path:ComputeAsync(zombie.root.Position,zombie.target.Position)
	local waypoints = path:GetWaypoints()
	local currentTarget = zombie.target

	local distance 
	--print(waypoints)
	for i, v in pairs(waypoints) do

		if v.Action == Enum.PathWaypointAction.Jump then
			zombie.human.Jump = true
		else
			zombie.human:MoveTo(v.Position)

			zombie.human.MoveToFinished:Wait()


			if not zombie.target then
				print("no zombie.target")

				break
			elseif checkDist(currentTarget,waypoints[#waypoints]) > 10 or currentTarget ~= zombie.target and zombie.target:FindFirstChild("Playing") then
				pathToTarget(zombie)

				print("pathToTarget")
			
				if v.Action == Enum.PathWaypointAction.Jump then
					zombie.human.Jump = true
					zombie.human:MoveTo(zombie.target.Position)
				end

				break
			end
		end
	end
end
1 Like

How often is the pathToTarget function ran? Perhaps it is stuttering the NPC on the last waypoint.

You could raycast before computing the path to see whether the zombie can be moved in a straight line to reach the player, if so, you can call MoveTo right away.

A good idea might be also to call MoveTo with the current position + a frame forward and repeat it while the path is being computed.

If these 2 solutions don’t give good enough results, try increasing the WaypointSpacing property.

It’s in a while loop that checks for all zombies in the workspace.

tried raycasting and waypoint spacing. can you elaborate on the second recommendation

Try using task.wait maybe? It could be the result of lag.

turns out it was an issue with the default Roblox animation script. I configured the idle to be compatible with the script. thanks for the help anyway!

What exactly did you modify in the default Roblox animation script? I’m currently having the same issue