Pathfinding does not choose the most optimal path

Hello, I’m making a turn-based game where the distance traveled costs AP.

While I was working on this mechanic, I noticed that the pathfinding service isn’t making the most optimal path. The video shows that it chooses the longest path, and there’s a gap of about 15 studs during which it chooses the longest path.

I tried changing AgentParams, changing PathfindingUseImprovedSearch, and when I set it to false, things get worse, and the gap starts to be 50 studs.
Can anyone help?

Sorry if it’s not entirely clear, I’m using Google Translate.

(Distance is in output)

1 Like

Can anyone help please? I still haven’t found a solution and I don’t know if this is a problem with Roblox Studio itself or if I’m doing something wrong.

this is a known limitation of PathfindingService’s grid/voxel system (~4 stud cells), open areas can get slight zigzags along voxel boundaries instead of a straight line, which matters a lot for a turn-based AP system

WaypointSpacing is a real AgentParameters field (default 4, from the official docs), lowering it can sometimes help but it’s not guaranteed since the grid search itself is what picks the route

for something more reliable: do a workspace:Raycast() straight from start to target first, if there’s clear line of sight just skip PathfindingService and use the straight line directly. for longer paths, post-process waypoints with raycasts between non-adjacent points instead of pure distance-based simplification, if point A can see point C with no obstacle in between you can drop everything between them and actually shorten the route, not just smooth it

if your AP system needs exact distances every time, building your own lightweight grid-based A* for this specific case is probably the most reliable long term fix since PathfindingService prioritizes speed over guaranteed shortest path

1 Like