Pathfind taking the shortest route rather than the path with the lowest cost

I’m not going to mark this as a bug, but rather support because I suppose I’m doing something wrong.

My issue: I’m trying to make a GPS-like system that takes a start and the destination and use the right side of the road to get there. The lanes have a cost of 1 while the baseplate and the concrete itself have a cost of 20 (to ensure the car doesn’t just drive in the median). I used pathfinding links which have BiDirectional disabled so the cars drive on the right side.
Here is the route the PathfindingService created [in red]
My desired path in green


In the photo: the service was told to create two different paths: one that starts at the beginning and goes to the first goal, and one that goes from the first goal to the second. Instead of using the road when possible, it just took the shortest path.
Here is my code below.

local pathfind=game:GetService('PathfindingService')
local path=pathfind:CreatePath({WaypointSpacing=1,AgentCanJump=false,AgentCanClimb=false,Costs={Road=1,Plastic=20,Concrete=20}})

path:ComputeAsync(script.Parent.Rig.HumanoidRootPart.Position,script.Parent.goal.Position)
for _, point in path:GetWaypoints()do
	script.Parent.Rig.Humanoid:MoveTo(point.Position)
	script.Parent.Rig.Humanoid.MoveToFinished:Wait()
end

path:ComputeAsync(script.Parent.Rig.HumanoidRootPart.Position,script.Parent.goal2.Position)
for _, point in path:GetWaypoints()do
	script.Parent.Rig.Humanoid:MoveTo(point.Position)
	script.Parent.Rig.Humanoid.MoveToFinished:Wait()
end

I appreciate all of your input! Thanks in advance.

Try cranking the cost of the concrete and baseplate up to something like 1000000000 just to see if that makes a difference.

Nope, still takes the shortest route

Did you have a read through the tutorial? It’s pretty good compared to most of theirs.

Yep, I’ve read through the whole documentation. I’ll give it another glance.

local pathfind = game:GetService("PathfindingService")
local path = pathfind:CreatePath({
	AgentCanJump = false,
	AgentCanClimb = false,
	WaypointSpacing = 1,
	Costs = {
		Road = 1,
		Plastic = 100,
		Concrete = 100
	}
})

function go(target)
	path:ComputeAsync(script.Parent.Rig.HumanoidRootPart.Position, target.Position)
	for _, pt in path:GetWaypoints() do
		script.Parent.Rig.Humanoid:MoveTo(pt.Position)
		script.Parent.Rig.Humanoid.MoveToFinished:Wait()
	end
end

go(script.Parent.goal)
go(script.Parent.goal2)

Roblox’s PathfindingService does not guarantee selection of the lowest total cost path… It finds the shortest navigable route unless obstacle costs dramatically affect it. Your cost table influences the pathing only when multiple paths exist, and all else is equal.

Key word here is dramatically.

This may be too tight … WaypointSpacing = 1, You’ll have to play with it.

1 Like

Isn’t this just my code but neater and higher costs for stuff that isn’t a road? Thanks for your input but unfortunately this doesn’t fix the problem

I found out that it generates the costs on a 1x1 grid regardless of what the WaypointSpacing is, so in order for it to work correctly I have to apply a grid to all of the roads

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.