PathfindingService does not work even when the target point is right there

I’m trying to make a system that creates paths to set points that smartly navigate around obstacles using PathfindingService.

Generally, this works pretty well. However, in a medium-sized map with 6 points, usually 1 or 2 come up as failed and return NoPath.

Most strangely is that it still seems to fail even when the target point is right in front of me, no invisible walls exist, and the floor is one unbroken part. Sometimes moving to the other side of the part allows it to work, with other paths even crossing the seemingly ‘inaccessible’ area.

Here’s my code:

local pfs = game:GetService("PathfindingService")
local tb = {game.Workspace.Part1, game.Workspace.Part2}
local plr = game.Workspace.Fro_ZinOvr
for i = 1, #tb do
	print(tb[i])
	print((plr.HumanoidRootPart.Position - tb[i].Position).Magnitude)
	local path = pfs:CreatePath({
		AgentRadius = 2.5,
		AgentHeight = 2.5,
		AgentCanJump = false,
	})
	local success, errorMessage = pcall(function()
		path:ComputeAsync(plr.HumanoidRootPart.Position, tb[i].Position)
	end)
	print(success)
	print(path.Status)
	if success and path.Status == Enum.PathStatus.Success then
		local way = path:GetWaypoints()
		for i = 1, #way do
			local x = Instance.new("Part")
			x.Parent = m
			x.Anchored = true
			x.CanCollide = false
			x.Position = way[i].Position
			x.Shape = "Cylinder"
			x.Orientation = Vector3.new(0, 0, 90)
			x.Size = Vector3.new(1, 5, 5)
		end
	end
end

My best guess is maybe something with the parameters, but I have tried experimenting with them and it did not change the result. If anyone can help figure this out, then that’d be really helpful!

2 Likes

I guess it fails because the lenght to the point is less than the AgentRadius. You could maybe fix this with something like this:

-- Lua Pseudo Code
if path.Status == Enum.PathStatus.NoPath then
    local DistanceToPoint = (EndPoint.Position - NPC.Position).magnitude
    if DistanceToPoint <= AgentRadius then
        NPC.Humanoid:MoveTo(EndPoint.Position)
    end
end