So I have been doing stuff with path finding, and I was following this tutorial,
so every time I try to run this. My code prints “Path failed to compute, retrying…”
This always happens and I cant figure out why.
So how do I fix it?
Code
Where it is happening:
local function ComputePath(startGoal, endGoal)
local po1 = startGoal.Position
local po2 = endGoal.Position
print(po1,po2)
local Path:Path
local RetriesLeft = 20 -- 20 retries
local RetryInterval = 3 -- delay 3 seconds each time it fails
for i = RetriesLeft, 1, -1 do
Path = pathFindingService:CreatePath()
Path:ComputeAsync(po1, po2)
if Path.Status == Enum.PathStatus.Success then
return Path
else
warn("Path failed to compute, retrying...")
task.wait(RetryInterval)
end
end
error("Path failed to compute.") -- this will be ran when the for loop is finished, or when all the retries failed.
end
Whole code:
-- events
local events = game:GetService('ReplicatedStorage'):FindFirstChild('NPCs events')
local setupBomb = events:FindFirstChild('SetupBomb')
-- entries
local entriePoints = workspace:FindFirstChild('Entrances')
local EPNumber = #entriePoints:GetChildren()
-- services
local pathFindingService = game:GetService('PathfindingService')
-- other
local visible = true
-- create a path!
local function ComputePath(startGoal, endGoal)
local po1 = startGoal.Position
local po2 = endGoal.Position
print(po1,po2)
local Path:Path
local RetriesLeft = 20 -- 20 retries
local RetryInterval = 3 -- delay 3 seconds each time it fails
for i = RetriesLeft, 1, -1 do
Path = pathFindingService:CreatePath()
Path:ComputeAsync(po1, po2)
if Path.Status == Enum.PathStatus.Success then
return Path
else
warn("Path failed to compute, retrying...")
task.wait(RetryInterval)
end
end
error("Path failed to compute.") -- this will be ran when the for loop is finished, or when all the retries failed.
end
-- make the NPC walk to it
local function WalkHumanoid(humanoid:Humanoid, startGoal:Vector3, endGoal:Vector3)
local Path = ComputePath(startGoal, endGoal)
-- setting up path properties
local Waypoints = Path:GetWaypoints()
local CurrentWaypointIndex = 2 -- the point to go after the first point
local MovedToFinishConnection:RBXScriptConnection
MovedToFinishConnection = humanoid.MoveToFinished:Connect(function(reached)
if reached then -- if the humanoid reached the waypoint within 8 seconds
if CurrentWaypointIndex < #Waypoints then -- if we have not cycle through the last waypoint
CurrentWaypointIndex += 1 -- we increase the index by 1 so that the humanoid moves to the next waypoint
humanoid:MoveTo(Waypoints[CurrentWaypointIndex].Position) -- and the same thing
if Waypoints[CurrentWaypointIndex].Action == Enum.PathWaypointAction.Jump then
humanoid.Jump = true
end
else -- we cycled the whole path series!
print("Path reached!")
MovedToFinishConnection:Disconnect() -- disconnect the unnecessary event to avoid memory leaks
end
else -- failed to reach waypoint within 8 seconds, the dummy probably is stuck, so let's recompute it!
MovedToFinishConnection:Disconnect() -- disconnect the unnecessary event to avoid memory leaks
WalkHumanoid(humanoid, startGoal.Position, endGoal) -- we are changing our start goal and replacing it to the current position of our dummy instead of reusing the old one
end
end)
-- visualize waypoints
if visible then
for _, point:PathWaypoint in ipairs(Waypoints) do
local Part = Instance.new("Part")
Part.Anchored = true
Part.CanCollide = false
Part.Material = Enum.Material.Neon
Part.Color = Color3.fromRGB(255,255,255)
Part.Position = point.Position -- PathWaypoint objects have a property named Position which describes their position in the form of Vector3.
Part.Parent = workspace
end
end
-- make the humanoid walk
humanoid:MoveTo(Waypoints[CurrentWaypointIndex].Position)
if Waypoints[CurrentWaypointIndex].Action == Enum.PathWaypointAction.Jump then
humanoid.Jump = true
end
end
local function sendToPlace(npc,endPoint) -- this is what sends the NPC to that certain place!
local StartingPoint = npc.PrimaryPart
local hum = npc:FindFirstChild('Humanoid')
-- move the NPC to the place.
WalkHumanoid(hum,StartingPoint,endPoint)
end
local function enter(npc) -- make an NPC enter the building!
print('Making '..npc.Name..' enter the place!')
local entryPoint = entriePoints:GetChildren()[math.random(1,EPNumber)]
print('Entering at '..entryPoint.Name)
sendToPlace(npc,entryPoint)
end
setupBomb.Event:Connect(function(NPC)
enter(NPC)
end)
I played around with the code.
Here is the full code now.
-- events
local events = game:GetService('ReplicatedStorage'):FindFirstChild('NPCs events')
local setupBomb = events:FindFirstChild('SetupBomb')
-- entries
local entriePoints = workspace:FindFirstChild('Entrances')
local EPNumber = #entriePoints:GetChildren()
-- services
local pathFindingService = game:GetService('PathfindingService')
-- other
local visible = true
-- create a path!
local function ComputePath(startGoal, endGoal)
local po1 = startGoal
local po2 = endGoal
local Path:Path
local RetriesLeft = 20 -- 20 retries
local RetryInterval = 3 -- delay 3 seconds each time it fails
for i = RetriesLeft, 1, -1 do
Path = pathFindingService:CreatePath()
Path:ComputeAsync(po1, po2)
if Path.Status == Enum.PathStatus.Success then
return Path
else
warn("Path failed to compute, retrying...")
task.wait(RetryInterval)
end
end
error("Path failed to compute.") -- this will be ran when the for loop is finished, or when all the retries failed.
end
-- make the NPC walk to it
local function WalkHumanoid(humanoid:Humanoid, startGoal:Vector3, endGoal:Vector3)
local Path = ComputePath(startGoal, endGoal)
-- setting up path properties
local Waypoints = Path:GetWaypoints()
local CurrentWaypointIndex = 2 -- the point to go after the first point
local MovedToFinishConnection:RBXScriptConnection
MovedToFinishConnection = humanoid.MoveToFinished:Connect(function(reached)
if reached then -- if the humanoid reached the waypoint within 8 seconds
if CurrentWaypointIndex < #Waypoints then -- if we have not cycle through the last waypoint
CurrentWaypointIndex += 1 -- we increase the index by 1 so that the humanoid moves to the next waypoint
humanoid:MoveTo(Waypoints[CurrentWaypointIndex].Position) -- and the same thing
if Waypoints[CurrentWaypointIndex].Action == Enum.PathWaypointAction.Jump then
humanoid.Jump = true
end
else -- we cycled the whole path series!
print("Path reached!")
MovedToFinishConnection:Disconnect() -- disconnect the unnecessary event to avoid memory leaks
end
else -- failed to reach waypoint within 8 seconds, the dummy probably is stuck, so let's recompute it!
MovedToFinishConnection:Disconnect() -- disconnect the unnecessary event to avoid memory leaks
WalkHumanoid(humanoid, startGoal, endGoal) -- we are changing our start goal and replacing it to the current position of our dummy instead of reusing the old one
end
end)
-- visualize waypoints
if visible then
for _, point:PathWaypoint in ipairs(Waypoints) do
local Part = Instance.new("Part")
Part.Anchored = true
Part.CanCollide = false
Part.Material = Enum.Material.Neon
Part.Color = Color3.fromRGB(255,255,255)
Part.Position = point.Position -- PathWaypoint objects have a property named Position which describes their position in the form of Vector3.
Part.Parent = workspace
game:GetService('Debris'):AddItem(Part,5)
end
end
-- make the humanoid walk
humanoid:MoveTo(Waypoints[CurrentWaypointIndex].Position)
if Waypoints[CurrentWaypointIndex].Action == Enum.PathWaypointAction.Jump then
humanoid.Jump = true
end
end
local function sendToPlace(npc,endPoint) -- this is what sends the NPC to that certain place!
local StartingPoint = npc.PrimaryPart.Position
local hum = npc:FindFirstChild('Humanoid')
-- move the NPC to the place.
WalkHumanoid(hum,StartingPoint,endPoint)
end
local function enter(npc) -- make an NPC enter the building!
print('Making '..npc.Name..' enter the place!')
local entryPoint = entriePoints:GetChildren()[math.random(1,EPNumber)]
print('Entering at '..entryPoint.Name)
sendToPlace(npc,entryPoint.Position)
end
setupBomb.Event:Connect(function(NPC)
enter(NPC)
end)
Hello! The creator of the tutorial here, I deeply apologize for the issues you have faced when you are trying to learn pathfinding from my tutorial. The scripts in it are outdated and inefficient, so please expect a complete revamp soon!