Hello everyone. I’m very new to Roblox development and was testing out the Pathfinding article @ Character Pathfinding. I was wondering if someone might provide some insight as to why the Path.Blocked event isn’t getting fired in the below example. I’ve searched all over for any examples of this same issue and have come up with nothing.
Below you can see the path generated by the PathfindingService’s GetWaypoints() function. PathfindingService works great and generates efficient paths even when I place the Red target behind a complicated maze (although below it’s just a straight line). Pretty sweet.
The issue I’m having trouble getting my head around is why path.Blocked:Connect(onPathBlocked)
isn’t firing when the above path gets blocked. Here’s how Path.Blocked is wired up in the script (again, this is just the script used in the Pathfinding article @ Oops!
local function onPathBlocked(blockedWaypointIndex)
warn("onPathBlocked at blockedWaypointIndex: ", 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)
To test the above Path.Blocked event, I added a 5-second wait after the above path is generated, then Instanced a wall directly in the path, as you can see below:
As you can see in the capture above, the Path.Blocked event isn’t fired after the path is blocked. The onPathBlocked
function is supposed to run, which then calls followPath()
to generate a new path. But the Path.Blocked event is never fired, no new path is generated, and the Dummy running the script just runs into the wall and gets stuck. Poor dumb Dummy, I’m sorry I can’t make you smarter — be patient.
Below is the full script I used from Character Pathfinding. The only modification I’ve made is to Instance a wall to block the path after 5 seconds. Any insight into this puzzle would be greatly appreciated. Dummy wants to be smarter!
Many thanks in advance.
Full Script:
local PathfindingService = game:GetService("PathfindingService")
-- Variables for the zombie, its humanoid, and destination
local zombie = script.Parent
local humanoid = zombie.Humanoid
local destination = game.Workspace.Red
-- Create the path object
local path = PathfindingService:CreatePath()
-- Variables to store waypoints table and zombie's current waypoint
local waypoints
local currentWaypointIndex
function showPath(points)
for _, waypoint in pairs(points) do
local part = Instance.new("Part")
part.Shape = "Ball"
part.Material = "Neon"
part.Size = Vector3.new(0.6, 0.6, 0.6)
part.Position = waypoint.Position
if waypoint.Action == Enum.PathWaypointAction.Jump then
part.Color = Color3.new(1,0,0)
end
part.Anchored = true
part.CanCollide = false
part.Parent = game.Workspace
end
end
local function followPath(destinationObject)
-- Compute and check the path
path:ComputeAsync(zombie.HumanoidRootPart.Position, destinationObject.Position)
-- Empty waypoints table after each new path computation
waypoints = {}
if path.Status == Enum.PathStatus.Success then
-- Get the path waypoints and start zombie walking
waypoints = path:GetWaypoints()
-- Move to first waypoint
currentWaypointIndex = 1
showPath(waypoints)
if waypoints[currentWaypointIndex].Action == Enum.PathWaypointAction.Jump then
humanoid.Jump = true
end
humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
else
if waypoints[currentWaypointIndex].Action == Enum.PathWaypointAction.Jump then
humanoid.Jump = true
end
-- Error (path not found); stop humanoid
humanoid:MoveTo(zombie.HumanoidRootPart.Position)
end
end
local function onWaypointReached(reached)
if reached and currentWaypointIndex < #waypoints then
currentWaypointIndex = currentWaypointIndex + 1
if waypoints[currentWaypointIndex].Action == Enum.PathWaypointAction.Jump then
humanoid.Jump = true
end
humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
end
end
local function onPathBlocked(blockedWaypointIndex)
warn("onPathBlocked at blockedWaypointIndex: ", 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)
-- Connect 'MoveToFinished' event to the 'onWaypointReached' function
humanoid.MoveToFinished:Connect(onWaypointReached)
followPath(destination)
wait(5)
local part = Instance.new("Part")
part.Shape = "Block"
part.Material = "ForceField"
part.Color = Color3.new(1,0,0)
part.Size = Vector3.new(4, 14.99, 63.15)
part.Position = Vector3.new(-48.13, 9.495, 92.035)
part.Parent = workspace