I want to measure distance between 2 points not in a straight line. How am I supposed to do this? Any ideas?
For example I want to track distance between those points within the red line, not the black one. I want a script which could automaticly find ways like that. Is a pathfinding system a solution? I’m an amateur in scripting. Please give me hints what tools it is the best to use for me.
yes you could generate a path using PathfindingService and then calculate the distance between each waypoint and the previous waypoint, the total distance would be the sum of all of those.
You could give every material, except the one the road is made of, a ridiculous weight so the AI follows it. Or you could add invisible walls around the road. You could also just manually calculate the distance by making waypoints yourself and looping through those. Do you have other suggestions, or are you just here to bash other people?
I am making suggestions just that they take time because they are good
Place Attachment nodes at every turn/key point (put them under a folder like workspace.PathNodes
). Let the player click nodes in order to form a route; the script sums the segment lengths (turns included) for the total distance.
How to use
- Create a Folder
PathNodes
in Workspace. - Add Attachment objects at each turn/spot (they can be inside small Parts, that’s fine).
- Put the LocalScript below in StarterPlayerScripts.
- Left-click: add the nearest node to your route.
- Z: undo last node.
- R: clear route.
- Enter: print total distance (studs + meters).
-- LocalScript (StarterPlayerScripts)
local Players = game:GetService("Players")
local UIS = game:GetService("UserInputService")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local PATH_FOLDER = workspace:WaitForChild("PathNodes")
local SELECT_RADIUS = 12 -- studs; how close your click must be to a node
-- collect all Attachment nodes
local nodes = {}
for _, inst in ipairs(PATH_FOLDER:GetDescendants()) do
if inst:IsA("Attachment") then
table.insert(nodes, inst)
end
end
-- selected route (array of Attachments)
local route = {}
local function nearestNode(worldPos: Vector3)
local best, bestDist = nil, math.huge
for _, att in ipairs(nodes) do
local d = (att.WorldPosition - worldPos).Magnitude
if d < bestDist then
bestDist, best = d, att
end
end
if bestDist <= SELECT_RADIUS then
return best
end
end
local function routeLengthStuds()
local sum = 0
for i = 2, #route do
local a = route[i - 1].WorldPosition
local b = route[i].WorldPosition
sum += (b - a).Magnitude
}
return sum
end
local function printTotal()
local studs = routeLengthStuds()
local meters = studs * 0.28 -- approx. 1 stud ≈ 0.28 m
print(string.format("Nodes: %d | Distance: %.1f studs (≈ %.1f m)", #route, studs, meters))
end
-- click to add nearest node
mouse.Button1Down:Connect(function()
local hitPos = mouse.Hit and mouse.Hit.Position
if not hitPos then return end
local n = nearestNode(hitPos)
if n then
table.insert(route, n)
print(("Added node: %s"):format(n.Name ~= "" and n.Name or tostring(n)))
printTotal()
else
print("No node near click (try closer).")
end
end)
-- Z to undo last, R to clear, Enter to print final
UIS.InputBegan:Connect(function(input, gp)
if gp then return end
if input.KeyCode == Enum.KeyCode.Z then
if #route > 0 then
local removed = table.remove(route)
print(("Removed: %s"):format(removed.Name ~= "" and removed.Name or tostring(removed)))
printTotal()
end
elseif input.KeyCode == Enum.KeyCode.R then
table.clear(route)
print("Route cleared.")
elseif input.KeyCode == Enum.KeyCode.Return then
printTotal()
end
end)
That’s it—click nodes along the path you’d take (turns included), and the script totals the polyline length. If you later want auto-path distance (e.g., shortest path between two nodes), you can build on this by adding a small adjacency table (graph) and run Dijkstra; but for a quick, visible “turn-by-turn” distance, clickable Attachments work great.
Yeah no, PathfindingService would probably be one of the better and easier methods to do this. I’d personally roll with Pathfinding service over making an overly complicated system to do the exact same task.
Just add some parts and do some simple calculation. It would probably take less code than pathfinding and pathfinding is asynchronous
Thanks, that’s helpful. I’ll probably try it.
i think you could add some waypoints, then calculate the magnitude of all these waypoints and add them all together, kinda like this
local distance = (waypoint1.Magnitude + waypoint2.Magnitude + waypoint3.Magnitude)
The way I would do it is indeed with pathfinding; but I wouldn’t use Roblox’. I would add convert the map into a graph. That means adding nodes to each bend of your road so that between any 2 points it’s straight. Then you can just use graph maths, A*, Dijkstra etc.