Pathfinding Frustration

So I’m having some serious issues with the pathfinding service. Initially I was using the :FindPathAsync() because I wasn’t aware they changed pathfinding again. It was working ok, except my paths would always go straight through walls. After reading up, I learned of the new syntax, CreatePath() and then ComputeAsync().

Except I have one small problem. :ComputeAsync() is constantly returning nil. I can’t call :GetWaypoints on whatever it returns, and the entire script breaks.

local path = pathfindingService:CreatePath()
function GetPath(Target)
	local p = path:ComputeAsync(script.Parent.HumanoidRootPart.Position,Target)
	local points = p:GetWaypoints()
	return points
end
GetPath(Vector3.new(10,10,10))

No matter what I pass as the Target point, it will always error on

local points = p:GetWaypoints()

and the error is always: “Attempt to index a nil value”

If I’m not mistaken this is due to the fact ComputeAsync is an Asynchronous function, so maybe try adding a wait() after it. Honestly I’m not too sure since a bunch of roblox functions have Async at the end which normally would mean Asynchronous but for some reason api says it yields.

Adding a wait returns the exact same error on the same line.

If it makes any difference, ComputePath is return a path. It’s path status is Enum.PathStatus.Success.

Calling :GetWaypoints() still immediately errors.

Try this,

local path = pathfindingService:CreatePath()
function GetPath(Target)
	local p = path:ComputeAsync(script.Parent.HumanoidRootPart.Position,Target)
        if path.Status == Enum.PathStatus.Success then
	local points = p:GetWaypoints()
	return points
        end -- not indented since written in devforum
end
GetPath(Vector3.new(10,10,10))

I did try that, same error, same line.

I’m not sure what’s going on. :GetWaypoints() just doesn’t work. Like at all.

Yeah, not sure either. I don’t really like Pathfinding because of its issues. Can you try printing p?

I FOUND IT!


You’re trying to call GetWaypoints on p instead of path.

Ok so, I just tried doing

for i,v in pairs(p) do print(tostring(i),tostring(v)) end 

Expecting to at least see it print “Status, Enum.PathStatus.Success” but instead it said p was a nil value. So I can confirm it’s status in a conditional, but any other function or line views p as nil.

Yes because p is the computeAsync, whereas path is just the general path. I’ve tried changing the variable names to directly replicate the wiki, it still gives me the same error on the same line every time.

No, computeasync doesn’t seem to return anything. It should be

local path = pathfindingService:CreatePath()
function GetPath(Target)
	path:ComputeAsync(script.Parent.HumanoidRootPart.Position,Target)
	local points = path:GetWaypoints()
	return points
end
GetPath(Vector3.new(10,10,10))

OHHHH. Let me try that.

I really wish they didn’t change pathfinding service every 6 months.

1 Like

Ok so now it seems to work again, but it has the exact same issue as when I was using the deprecated syntax.

It still makes paths directly through walls and my NPCs just get stuck.

Yeah I remember testing pathfinding through a maze and it worked except it got stuck on one part, I believe I just removed that part. Pretty sure that’s just pathfinding’s fault.

Can you send an image of the part it’s getting stuck on?

Here’s an image of the path

I’m thinking I’ll just have to make some kind of blocked path detection with raycasting, because the pathfinding service can’t seem to handle these thin fences.

Did you try the path.Blocked event?

I can see that it’s a function but the API doesn’t mention what it’s used for or how to use it.

Here’s what’s on the article,


local function onPathBlocked(blockedWaypointIndex)
	-- Check if the obstacle is further down the path
	if blockedWaypointIndex > currentWaypointIndex then
		-- Call function to re-compute the path
		followPath(destination)
	end
end
 
-- Connect 'Blocked' event to the 'onPathBlocked' function
path.Blocked:Connect(onPathBlocked)