Pathfinding generating links between waypoints that are obstructed

Expected behaviour:
Pathfinding should link waypoints that can actually be reached by each other

Actual behaviour:
Pathfinding generates an impossible path by placing a seemingly random waypoint far below the terrain region, and connecting it with a waypoint on the terrain itself.

As seen in the image above, pathfinding has linked waypoints that can’t be reached by simply walking, causing the NPC to become stuck in an endless loop of trying to reach a waypoint it can’t, moving around randomly in an attempt to get un-stuck.

Studio has the “Show Navigation Mesh” option turned on, the only other path indicator is the slightly transparent green square that indicates the waypoint the NPC is trying to get to, called “Goal” under workspace.

It looks like there’s a deeper issue, infact. A bug where raycasting misses terrain on the borders of terrain chunks has been reported before, and I believe this is what’s happening in this case, some code is using raycasts to place a waypoint, the raycast misses the terrain and hits the ground below instead.

Place file:
npc smooth pathing.rbxl (57.5 KB)

Repro steps:
Have a path that crosses a terrain chunk boundary, that coincidentally has a waypoint directly over than boundary.

Impact: High
This can completely break NPCs on maps with terrain, as the waypoint becomes unreachable.

Workaround:
Re-generate the path every once in a while, so that NPCs falling off areas (and hopefully landing in a recoverable spot) will not get stuck, possibly add randomization to :MoveTo positions so you’re in the clear if the path regenerates while NPC is over a chunk border.