First sorry for my broken english. I’m trying to make a npc that goes selected destinations, everything works good but only problem is if the path is blocked first npc pass after that he can’t calculate the path and giving me error.
local hum = script.Parent
local humrp = script.Parent.Parent.HumanoidRootPart
local pathFindingService = game:GetService("PathfindingService")
local destinations = game.Workspace.Destinations
local path = pathFindingService:CreatePath()
local function getPath(destination)
local path = pathFindingService:CreatePath()
path:ComputeAsync(humrp.Position, destination.Position)
return path
end
local function walkTo(destination)
local path = getPath(destination)
path.Blocked:Connect(function(dest)
print("path blocked")
path = getPath((dest))
end)
for index, waypoint in pairs(path:GetWaypoints()) do
hum:MoveTo(waypoint.Position)
hum.MoveToFinished:Wait()
end
end
while true do
walkTo(destinations.dest1)
walkTo(destinations.dest2)
walkTo(destinations.dest3)
walkTo(destinations.dest4)
walkTo(destinations.dest5)
walkTo(destinations.dest6)
end
The error indicates that the object you are trying to get the position of is nil, so it either does not exist or was not assigned properly.
In your case, it means that dest is nil, so make sure that path.Blocked is returning a valid waypoint.
path.Blocked:Connect(function(dest)
print("path blocked")
if dest ~= nil then
path = getPath((dest))
else
----- find something to do if dest is nil, maybe a failsafe stop
end
end)
YOU ALSO CAN
Make it re-calculate path when it detects that it is blocked
local function walkTo(destination)
local path = getPath(destination)
path.Blocked:Connect(function(dest)
print("path blocked")
path:ComputeAsync(humrp.Position, destination) ---- re-calculate path no need to re-assign path
if path.Status == Enum.PathStatus.Succeess then
-- Retrieve update waypoint list with path:GetWaypoints()
-- and Continue walking towards target
--- you can try re-using MoveTo with the re-computed path here
else
-- Error, path not found
end
end)
for index, waypoint in pairs(path:GetWaypoints()) do
hum:MoveTo(waypoint.Position)
hum.MoveToFinished:Wait()
end
end
I am unsure if that properly solves your issue since i haven’t used pathfinding too much.
But this should atleast help you understand what the error is.