Pathfinding Service Performance Degradation

Hello all :slight_smile: Thanks for taking a look here.

Below I have an example pathfinder using roblox’s service. In short, I use the first waypoint returned by the service, set my model’s velocity in that direction, then find a new set of way points roughly every tenth of a second.

The performance and script monitor do not report any latency or slowness, however I have noticed that after ~ 1000 iterations or so the waypoint updates start happening significantly slower - the loop is still called, but waypoint.position is nil. It becomes nil most of the time to all of the time past 10,000 ish loops, and becomes nil faster if a player is being tracked/is the target of the wayfinder.

it’s also worth noting in the commented out portion, the ‘marked’ path is still drawn, and path:GetWaypoints() still returns all the waypoints I wanted, even though waypoints[1].Position returns nil.

This results in the ‘mob’ moving slower and slower until it doesn’t move at all.

Any suggestions?

		
		local function createWaypoints()
			
			local waypointMarkers = game.Workspace.WaypointMarkers:GetChildren()
			if #waypointMarkers > 0 then
				for i,v in ipairs(waypointMarkers) do
					v:Destroy()
				end	
			end

			local path = Pathfinder:CreatePath({ AgentHeight = 6, AgentRadius = 8, AgentCanJump = false, WaypointSpacing = 8 })
			local start = mob.PrimaryPart.Position
			local ending = target.Position
			local startVector = Vector3.new(start.X, 0, start.Z)
			local endVector = Vector3.new(ending.X, 0, ending.Z)

			path:ComputeAsync(startVector, endVector)

			local waypoints = path:GetWaypoints()
			local nextPoint
			
			local success, response = pcall(function()
				nextPoint = waypoints[1].Position
				local TravelVector = (target.Position - nextPoint).Unit
				mob.PrimaryPart.XZVelocity.Velocity = Vector3.new(TravelVector.X*mobSpeed, 0, TravelVector.Z*mobSpeed)
			end)
			if not success then
				print("pathing failure")
			end
			
			

			--for _, waypoint in ipairs(waypoints) do

			--	local part = Instance.new("Part")
			--	part.Shape = "Ball"
			--	part.Material = "Neon"
			--	part.Size = Vector3.new(1,1,1)
			--	part.Position = waypoint.Position
			--	part.Anchored = true
			--	part.CanCollide = false
			--	part.Parent = game.Workspace.WaypointMarkers
				
				
				
			--end

		end
		createWaypoints()

I have solved this problem. My while loop was generating new instances of the function I was using, causing conflicting values.

Control your while loops kids…

2 Likes