I’d like for an Avatar to be able to use Pathfinding Service to reach a goal without slamming into and shoving other Avatars in its way.
I created a narrow corridor with 3 avatars blocking the path. I then used pathfinding to tell the avatar to walk to the end of the corridor, and it ignored the 3 avatars in its way and got stuck on them. This was with the blocking path detection in place. (My code is basically a copy-paste of the recommended pathfinding code in Roblox Developer Hub)
I tried using Pathfinding Modifiers in the HumanoidRootPart of each dummy, but this failed to dissuade the Pathing Dummy to choose a new path.
I’ve provided an example game. While the game is in Run mode, send a signal via Command Bar:
workspace.Pather.Move:Fire(workspace.Point)
The script is inside the BindableEvent inside the Pather model. Here is the code:
Code
local PathfindingService = game:GetService("PathfindingService")
local Path = PathfindingService:CreatePath()
local Remote = script.Parent
local Model = Remote:FindFirstAncestorWhichIsA("Model")
local Humanoid = Model:WaitForChild("Humanoid")
local waypoints
local nextWaypointIndex
local reachedConnection
local blockedConnection
local function MoveAlongPath(target)
local success, errorMessage = pcall(function()
Path:ComputeAsync(Model:GetPivot().Position, target:GetPivot().Position)
end)
if success and Path.Status == Enum.PathStatus.Success then
-- Get the path waypoints
waypoints = Path:GetWaypoints()
-- Detect if path becomes blocked
blockedConnection = Path.Blocked:Connect(function(blockedWaypointIndex)
-- Check if the obstacle is further down the path
if blockedWaypointIndex >= nextWaypointIndex then
-- Stop detecting path blockage until path is re-computed
-- Unfortunately this won't stop units with respecting each others positions.
blockedConnection:Disconnect()
-- Call function to re-compute new path
MoveAlongPath(target)
end
end)
-- Detect when movement to next waypoint is complete
if not reachedConnection then
reachedConnection = Humanoid.MoveToFinished:Connect(function(reached)
if reached and nextWaypointIndex < #waypoints then
-- Increase waypoint index and move to next waypoint
nextWaypointIndex += 1
Humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
else
reachedConnection:Disconnect()
blockedConnection:Disconnect()
end
end)
end
-- Initially move to second waypoint (first waypoint is path start; skip it)
nextWaypointIndex = 2
Humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
else
warn("Path not computed!", errorMessage)
end
end
Remote.Event:Connect(MoveAlongPath)
RudestPathfindingAvatar.rbxl (84.4 KB)