So I’m trying to find the total distance between multiple part in a table as shown below.
local path = {}
local currentPoint = spawnPart
--select a path
while #currentPoint:GetChildren() > 0 do
table.insert(path, currentPoint:GetFullName())
local nextPoint = currentPoint:GetChildren()[math.random(1, #currentPoint:GetChildren())]
currentPoint = nextPoint
end
--calculate total path length
local totalPathLength = 0
for i = 1, #path - 1 do
totalPathLength = totalPathLength + (path[i + 1] - path[i]).Magnitude
end
The problem is the path[i+1] will return nil on the last one. The solution that I came up with is just to pcall() that part but I think there’s a better solution or method to do this…?
GetFullName returns a string not an instance so you can’t use it to get the distance between two part instances. table.insert(path, currentPoint) --insert part into array instead