PathfindingLink causes PathfindingService to ignore the WaypointSpacing parameter in certain path segments

Reproduction Steps

The image below illustrates a set-up where the WaypointSpacing parameter is ignored. There is an orange arrow between two green attachments to illustrate where the PathfindingLink is used.

bug_situation

This bug always happens exactly from the end of a PathfindingLink until the next corner of the path. When you use Path:ComputeAsync() to generate a path between the starting point (green part on the right) and the end point (yellow part on the left), an array of waypoints will be returned which are missing some indexes.

This is the code I use to generate and visualize the path:

local PathfindingService = game:GetService("PathfindingService")
local Path: Path = PathfindingService:CreatePath({
	["AgentRadius"] = 2.5;
	["AgentHeight"] = 5;
	["AgentCanJump"] = false;
	["WaypointSpacing"] = 2;
})

Path:ComputeAsync(game.Workspace.StartPart.Position, game.Workspace.EndPart.Position)

local waypoints = Path:GetWaypoints()
for i = 1, #waypoints do
	local P = Instance.new("Part")
	P.TopSurface, P.BottomSurface = Enum.SurfaceType.Smooth, Enum.SurfaceType.Smooth
	P.Anchored = true
	P.CanCollide = false
	P.Size = Vector3.new(1, 1, 1)
	P.BrickColor = BrickColor.Blue()
	P.CFrame = CFrame.new(waypoints[i].Position)
	P.Parent = game.Workspace
end

Expected Behavior


Below is an image of what I expect to happen; when a path is generated it should add additional waypoints in the array returned by Path:GetWaypoints() that are in between the end point of a PathfindingLink and the next corner on the path.

bug_expected

Actual Behavior


What actually happens is that waypoints between the end of a PathfindingLink and the next corner of the path are not included in the array returned by Path:GetWaypoints(). Below is an image illustrating the problem. Notice how on the left of the image, there is a blue waypoint isolated on its own. That waypoint is generated at the end of the PathfindingLink connection. There are no waypoints until the next corner to its left is reached, after which the WaypointSpacing parameter is correctly incorporated again.

bug_current

Workaround


Depending on how you use pathfinding, you may have to structure your code in a different way to achieve the same goal. One approach to pathfinding between waypoints is to use a for-loop and use Humanoid.MoveToReached:Wait() to wait until the next iteration:

local waypoints = Path:GetWaypoints()
for i = 1, #waypoints do
	AI.Humanoid:MoveTo(waypoints[i].Position) -- naive assumption that the AI can simply walk over a PathfindingLink to simplify this code
	AI.Humanoid.MoveToFinished:Wait() -- naive assumption that you always correctly reach the current waypoint
end

If the WaypointSpacing parameter is set to be small, the loop above will run very frequently. For example, with a WaypointSpacing of only 2 and a Humanoid WalkSpeed of 16, the loop above should in theory run 8 times per second. This can be used to add additional polling logic inside the same loop, such as checking if the AI should greet a nearby player or run faster when a monster is nearby. However, with this bug there may be moments when the for-loop will not run for a few seconds at a time, so additional polling code will have to be run in a separate loop. So you may have to refactor your code to look like this:

local waypoints = Path:GetWaypoints()
local endOfPathReached = true
task.spawn(
	function()
		for i = 1, #waypoints do
			AI.Humanoid:MoveTo(waypoints[i].Position) -- naive assumption that the AI can simply walk over a PathfindingLink to simplify the code
			AI.Humanoid.MoveToFinished:Wait() -- naive assumption that you always correctly reach the current waypoint
		end
		endOfPathReached = true
	end
)

while not endOfPathReached do
	-- run your polling logic here
	game:GetService("RunService").Heartbeat:Wait()
end

Issue Area: Engine
Issue Type: Other
Impact: Moderate
Frequency: Constantly
Date Last Experienced: 2022-09-02 00:09:00 (+02:00)

6 Likes

Hi, Thanks for reporting the bug. We are working on the fix.

6 Likes

Greetings! Any updates on the status of this bug? My games heavily use pathfinding-service and so having all of this fixed would be immensely reassuring!

1 Like

^ What he said is the same for most of us, an update would be nice.

Hi, This bug should be fixed now. Please let us know if you are still seeing such problem.
Please note Android latest build has not yet rolled out, so you might see this bug still there.