Pathfinding lagging behind, MoveToFinished alternative

Hello, I’m currently working on my first advanced AI program. I have noticed that when I use MoveToFinished the AI pauses in between each waypoint. I also read about how trash MoveToFinished was so I decided to use a repeat wait() method to see how far the AI is from each point and if he’s less than whatever the moveto ends:

--Pathfinding functions
local playerDetecting = false
local function getPath(destination)
	local pathParams = {
		["AgentHeight"] = 3,
		["AgentRadius"] = 6.5,
		["AgentCanJump"] = true
	}
	local path = pathfindingService:CreatePath(pathParams)
	path:ComputeAsync(enemy.HumanoidRootPart.Position, destination.Position)
	return path
end



--Attacking the player function
local function AttackingPlayer()
	stopAttack = false
	for _, track in pairs(enemy.Zombie:GetPlayingAnimationTracks()) do
		track:Stop()
	end
	crawlingAny:Play()
	if not enemy.Sounds.SpiderSounds.IsPlaying then enemy.Sounds.SpiderSounds:Play() end
	enemy.Zombie.WalkSpeed = 25
	repeat wait()
		local path = getPath(selectedPlayer.Character.HumanoidRootPart)
		if path.Status == Enum.PathStatus.Success then
			for index, waypoint in pairs(path:GetWaypoints()) do
				if waypoint.Action == Enum.PathWaypointAction.Jump then
					humanoid.Jump = true
				end
				humanoid:MoveTo(waypoint.Position)
				
				--MoveToFinished Wait Alternate
				repeat wait()
					troubleshooting(enemy.HumanoidRootPart.Position.X, enemy.HumanoidRootPart.Position.Z)
					if (enemy.HumanoidRootPart.Position - waypoint.Position).magnitude <= 2 then finishedWalking = true end
				until finishedWalking == true

				finishedWalking = false
			end	
		else
			stopAttack = true
		end
	until (enemy.HumanoidRootPart.Position - selectedPlayer.Character.HumanoidRootPart.Position).magnitude > 80 or selectedPlayer.Character.Humanoid.Health == 0 or stopAttack == true
	enemy.Zombie.WalkSpeed = 16
	finishedWalking = false
end

--Walking to waypoint, not player, function
local function walkToPath(destination, moveToFinishedWait)
	local path = getPath(destination)
	if path.Status == Enum.PathStatus.Success then
		crawlingAny:Play()
		enemy.Sounds.SpiderSounds:Play()
		idleAny:Stop()
		for index, waypoint in pairs(path:GetWaypoints()) do
			if waypoint.Action == Enum.PathWaypointAction.Jump then
				humanoid.Jump = true
			end
			humanoid:MoveTo(waypoint.Position)
			
			--MoveToFinished Wait Alternate
			repeat wait()
				raycasting()
				troubleshooting(enemy.HumanoidRootPart.Position.X, enemy.HumanoidRootPart.Position.Z)
				if raycastingInformation_RightSide[3] == true or raycastingInformation_LeftSide[3] == true or (enemy.HumanoidRootPart.Position - waypoint.Position).magnitude <= 2 then --IfPlayer Detected
					finishedWalking = true
				end
			until finishedWalking == true
			finishedWalking = false
			
			--If Player Detected
			if raycastingInformation_RightSide[3] == true or raycastingInformation_LeftSide[3] == true and playerDetecting == false then --IfPlayer Detected
				playerDetecting = true
				AttackingPlayer()
			end
			
		end
		idleAny:Play()
		crawlingAny:Stop()
		enemy.Sounds.SpiderSounds:Stop()
	end
end

The troubleshooting function takes the position, X and Z, of his rootPart just to see if he’s stuck. Still working on that but even cancelling that function results in him lagging. The walking function starts first and raycasts to see if there’s any players in his sight. If there is then he starts the attack function. It seems both walking, in both functions, ‘lag’. Here’s a video of what I mean:

Not sure where you was told movetofinished was trash but I have never had problems with it

Here you can see a video of me showing how I do path finding that might help you

It’s also possible to calculate how long you must wait for the NPC to reach the destination

local waitTime = magnitude / speed

task.wait(waitTime)

-- move to next point
4 Likes

Interesting… i’ll definitely be watching this video to learn more about this. Thank you

Excellent in depth video, nice work.