PathfindingService is pretty garbage

Trying to use PathfindingServiec however running into constant issues with pathfinding not working as the developer hub says it would. It doesn’t take a direct route and it doesn’t take into account blocked paths
ezgif.com-video-to-gif (1)

This is more or less copy pasted from here

local PathfindingService = game:GetService("PathfindingService")

local waypoints
local nextWaypointIndex
local reachedConnection
local blockedConnection

local SetPath = PathfindingService:CreatePath()

task.wait(2)

local function followPath(destination)

	print(destination)
	-- Compute the SetPath
	local success, errorMessage = pcall(function()
		SetPath:ComputeAsync(workspace.Rig:GetPivot().Position, destination)
	end)

	if success and SetPath.Status == Enum.PathStatus.Success then
		-- Get the SetPath waypoints
		waypoints = SetPath:GetWaypoints()

		-- Detect if SetPath becomes blocked
		blockedConnection = SetPath.Blocked:Connect(function(blockedWaypointIndex)
			-- Check if the obstacle is further down the SetPath
			if blockedWaypointIndex >= nextWaypointIndex then
				-- Stop detecting SetPath blockage until SetPath is re-computed
				blockedConnection:Disconnect()
				-- Call function to re-compute new SetPath
				followPath(destination)
			end
		end)

		-- Detect when movement to next waypoint is complete
		if not reachedConnection then
			reachedConnection = workspace.Rig.Humanoid.MoveToFinished:Connect(function(reached)
				print(reached, nextWaypointIndex < #waypoints)
				if reached and nextWaypointIndex < #waypoints then
					-- Increase waypoint index and move to next waypoint
					nextWaypointIndex += 1
					workspace.Rig.Humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
				else
					workspace.Rig:PivotTo(workspace.Start.CFrame)
					
					reachedConnection:Disconnect()
					blockedConnection:Disconnect()
					
					SetPath = PathfindingService:CreatePath()
					
					waypoints = nil
					nextWaypointIndex = nil
					reachedConnection = nil
					blockedConnection = nil
					
					followPath(workspace.End.Position)
				end
			end)
		end

		-- Initially move to second waypoint (first waypoint is SetPath start; skip it)
		nextWaypointIndex = 2
		workspace.Rig.Humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
	else
		warn("Path not computed!", errorMessage)
	end
end

followPath(workspace.End.Position)

I’ve tried doing

local SetPath = PathfindingService:CreatePath({
	Costs = {
		Block = math.huge
	}
})

with a modifier inside the black part and Label set to Block. But this still does nothing
image

Honestly, I’ve looked into the pathfinding service and without experiencing these issues I found is way too confusing to implement, never got it to work properly.

To remedy this, you can keep recomputing the path at some appropriate time interval (e.g. 0.1 seconds) so if any obstructions are created after the initial path computation (like when you resize the part in the video), the agent won’t get stuck. This is what SimplePath does, and from my experience it’s better than using whatever is currently in the Roblox Docs.

2 Likes

The path-following code sample you are using doesn’t take into consideration waypoint actions, in particular jumping. That’s probably what’s causing the character to get stuck in front of the block.

You have several options:

(1) Disable jumping in the path request, so your character doesn’t assume that it can go over the big block.

local SetPath = PathfindingService:CreatePath({AgentCanJump = false})

(2) If jumping is enabled, set the action in humanoid before while moving to the next waypoint.

local shouldJump = waypoints[nextWaypointIndex].Action == Enum.PathWaypointAction.Jump 
workspace.Rig.Humanoid.Jump = shouldJump 

workspace.Rig.Humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
1 Like