:MoveTo() Lagging

Hi! I’m having some issues with path finding. Whenever I move my NPC to a waypoint, it pauses for a very short moment. This makes it ‘laggy’. The lag also seems to greatly increase whenever the NPC comes close to the player. This means my NPC will never reach the player, which I want it to. (Yes, the NPC’s walkspeed is higher than the player’s) Here is part of my script:

while true do
	
	local target = FindTarget()
	
	if target then
		
		local success, err = pcall(function()
			path:ComputeAsync(hrp.Position,target.PrimaryPart.Position)
		end)
		
		if success and path.Status == Enum.PathStatus.Success then
			
			local waypoints = path:GetWaypoints()
			
			for _, waypoint in ipairs(waypoints) do
				hum:MoveTo(waypoint.Position)
				if waypoint.Action == Enum.PathWaypointAction.Jump then
					hum.Jump = true
				end
				hum.MoveToFinished:Wait()
			end
			
		end
		
	end
	
	runServ.Heartbeat:Wait()
	
end

Any help is appreciated! Example:
robloxapp-20220305-1357550.wmv (607.0 KB)

at each point reached it stops for a short amount of time?

Yes. I sent a video showing what happens. If you have any idea of how to fix this, they’d be greatly appreciated.

I think it is because every time you move a new waypoint is created and it’s not in the table so it has to get the over and over again with the new waypoints created, i’ve never scripted any npc or something but if there’s anyway to see if there’s a new waypoint created you could get the new table and unite it with the old one and recreate the for-loop maybe. Maybe you can develop something from this idea i just gave you xd, hope i’m helpful.

I’ll try making a longer wait for the loop. Thanks.

Also, there is network ownership. My NPCs (which do not pathfind to the player, rather set points on the map) will stutter when the player comes near for a second, creating a lagging effect.

I did set the network owner to the server at the beginning, but I’ll try doing it in the loop. Thanks!
Edit: Unfortunately, it didn’t work.

If what you’ll try won’t work i think there’s some other way you can achieve this, you can try getting every point
the player changes direction and put the npc go to those points otherwise if the case is that the player just spawned in the world somewhere where the npc can’t reach then you use the path finding to get to the player till the point he was spawned at.

what i mean is, get the direction vector of the player and everytime it changes you get the position the player was at, while it doesn’t change the npc will follow the same direction.
And since the point has to be in the back of the player, subtract from player’s position it’s size and add the size of the npc so it will be in back, so techincally substract npc’s size.

1 Like

Yes, but then the NPC can’t walk around obstacles. I need it to hunt down the player.

You can also detect if the player jumps…

Turns out its the :MoveTo() function.

Any more ideas on how to fix this?

Calculate the exact time the npc will get there and substract an amount of time so the moveto could calculate the next path to the next point while the npc is still moving.

Use Pivotto Insread