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.
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))
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))
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?
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.
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)