How to Pathfind Around Avatars?

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)

You could try making 2 points, I’m guessing that would work

Unfortunately that wouldn’t solve the problem of it bumping into another Dummy, which is the main problem I want to solve.

The usecase is that it’s suppose to pathfind down the corridor while approaching other dummies on the way. The dummies are suppose to disappear after some combat, but if there’s even just two or three trying to approach a single target, they’ll bump and push and shove each other instead of finding an unoccupied spot close to the target to occupy instead.

When you did this, did you pass an agentParameters table to CreatePath()? The PathfindingModifiers will not do anything if you don’t specify their costs in the agentParameters table:

local agentParams = {
AgentRadius = 2,
AgentHeight = 5,
AgentCanJump = true,
  Costs = {
    <pathfinding modifier label name> = <number>
  }
}
local path = PathfindingService:CreatePath(agentParams)

Additionally, instead of placing the PathfindingModifier in the HumanoidRootPart, try creating an invisible part that covers the avatar entirely and weld it to the avatar’s torso. Then, place the PathfindingModifier in there instead (make sure CanCollide and CanTouch are set to false on the part).

I’m thinking how it will work (I’m stupid on codes and haven’t touched PC for a while) because if the pathfinding modifier “block” is inside the avatar and the cost is math.huge, would that just make an error, I’m not entirely sure.

No, won’t cause an error. math.huge just signifies that the volume encapsulated by the part should be avoided at all costs, even if it’s the only path available.

Make sure that “block” isn’t in the pathfinding avatar itself, though, just the ones you want to avoid.

1 Like

oh, I haven’t been on pathfinding, Thanks for a bit info.

1 Like

The only way I can think of doing this is by using the run service.

  1. Get all of the parts in the way of the avatar.
  2. Have the avatar move to a point above and in front of the avatar.
  3. Once the avatar has reached that point, have the avatar move down to the original spot.