Pathfinding difficulties

My pathfinding agent is having a uh, seizure, for lack of a better word.

function functions.patrol(statistics, entity, previousNode)
	local path = pathfindingService:CreatePath({
		AgentRadius = entityStats[statistics].pathfindingRadius,
		AgentHeight = entityStats[statistics].pathfindingHeight,
		AgentCanJump = false,
		AgentCanClimb = false,
		WaypointSpacing = 2
	})
	
	------------------------------------------------------------------------------------------------------------------------------

	--node selection
	local closestPathNodes = {}
	local selectedPathNodeKey
	local selectedPathNode

	--get 3 closest previous nodes
	for i, v in pairs(pathNodes) do
		local magnitude = (previousNode.Position - v.Position).Magnitude

		closestPathNodes[i] = {v, magnitude}
	end
	
	--sort
	for i = 1, #closestPathNodes do
		local minIndex = i
		for j = i+1, #closestPathNodes do
			if closestPathNodes[j][2] < closestPathNodes[minIndex][2] then
				minIndex = j
			end
		end

		closestPathNodes[i], closestPathNodes[minIndex] = closestPathNodes[minIndex], closestPathNodes[i]
	end
	
	--remove selected node
	table.remove(closestPathNodes, 1)
	
	--pick node
	for nodeValue = 1, #closestPathNodes do
		if nodeValue > 3 then
			table.remove(closestPathNodes, 4)
		end
	end

	--pick random node from 3 closest
	selectedPathNodeKey = math.random(#closestPathNodes)
	selectedPathNode = closestPathNodes[selectedPathNodeKey][1]
	
	------------------------------------------------------------------------------------------------------------------------------

	--pathfinding
	local waypoints
	local nextWaypointIndex
	local reachedConnection
	local blockedConnection

	local success, errorMessage = pcall(function()
		path:ComputeAsync(entity.HumanoidRootPart.CFrame.Position, selectedPathNode.Position)
	end)

	if success and path.Status == Enum.PathStatus.Success then
		waypoints = path:GetWaypoints()
		
		-- Get the path waypoints
		for _, waypoint in waypoints do
			local part = Instance.new("Part")
			part.Position = waypoint.Position
			part.Size = Vector3.new(0.5, 0.5, 0.5)
			part.Transparency = 0
			part.Color = Color3.fromRGB(255, 255, 0)
			part.Anchored = true
			part.CanCollide = false
			part.Name = "waypoint"
			part.Parent = entity.waypoints
		end

		-- Detect if path becomes blocked
		blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
			-- Check if the obstacle is further down the path
			if blockedWaypointIndex >= nextWaypointIndex then
				-- Stop detecting path blockage until path is re-computed
				blockedConnection:Disconnect()
				-- Call function to re-compute new path
				functions.patrol(statistics, entity)
				
				print("blocked")
			end
		end)

		-- Detect when movement to next waypoint is complete
		if not reachedConnection then
			reachedConnection = entity.Humanoid.MoveToFinished:Connect(function(reached)
				if reached and nextWaypointIndex < #waypoints then
					-- Increase waypoint index and move to next waypoint
					nextWaypointIndex += 1
					entity.Humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
				else
					reachedConnection:Disconnect()
					blockedConnection:Disconnect()
				end
			end)
		end
		
		--entity.Humanoid.WalkSpeed = statistics.walkspeed   --figure this out later

		-- Initially move to second waypoint (first waypoint is path start; skip it)
		nextWaypointIndex = 2
		entity.Humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
		
		--when finished
		entity.Humanoid.MoveToFinished:Connect(function()
			if nextWaypointIndex == #waypoints then
				entity.waypoints:ClearAllChildren()
				
				task.wait(1)
				
				functions.patrol(statistics, entity, selectedPathNode)
			end
		end)
	else
		warn("Path not computed!", errorMessage)
	end
end

Any ideas?

edit: didnt attach video oopsies

sorry for lack of quality cant use recording software atm

You might want to try the module simplepath. However, I am happy to debug your code if you would send your place file. By the looks of things, It can’t figure out which node is the closest.

1 Like

ill checkout simplepath, i wish to work on this alone minus small interventions, thank you for the offer though! (sorry for the late reply)

figured it out! just had to reset nextWaypointIndex

--when finished
entity.Humanoid.MoveToFinished:Connect(function()
	if nextWaypointIndex == #waypoints then
		entity.waypoints:ClearAllChildren()
				
		task.wait(1)
				
		nextWaypointIndex = 2 --bug fix :D
		functions.patrol(statistics, entity, selectedPathNode)
	end
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.