I’m trying to make a ordered list of parts, but the parts numerical placement in the table will be dependent on the magnitude of a starting point. How would I do this?
This is what I have so far.
local e = workspace.eeee
local Waypoints = workspace.Waypoint
local OrderedWaypoints = {}
local waypointDistances = {}
local startWaypoint = Waypoints:WaitForChild("RailWaypointStart")
local endWaypoint = Waypoints:WaitForChild("RailWaypointEnd")
for i, v in pairs(Waypoints:GetChildren()) do
if v.Name == "RailWaypoint" then
table.insert(waypointDistances, (startWaypoint.Position - v.Position))
end
end
for i, v in pairs(waypointDistances) do -- Where I'm going to order the parts
end
Is there a way for me to just return the parts in order that is closer than other parts in the same group? Because it only sorts their positions, but doesn’t tell me there names.
Instead of sorting a list of distances, you’ll want to sort a list of parts like so:
local railroadWaypoints = {}
for _,child in Waypoints:GetChildren() do
if child.Name == "RailWaypoint" then
table.insert(railroadWaypoints, child)
end
end
table.sort(railroadWaypoints, function(a, b)
local aDistanceFromStartWaypoint = (startWaypoint.Position - a.Position).Magnitude
local bDistanceFromStartWaypoint = (startWaypoint.Position - b.Position).Magnitude
return aDistanceFromStartWaypoint < bDistanceFromStartWaypoint
end)
With the sorted list of parts, you could then do something like this:
for index,waypoint in railroadWaypoints do
print(index, waypoint.Name)
end