Finding total distance of multiple part in a table?

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…?

what are you trying to achieve there? more information is needed honestly

Trying to find the total length of the distance between each part
image

This image should be clearer. I’m trying to find the distance of all the part combined
image

I don’t see anything wrong with the code, even the problem you gave doesn’t seem to exist since you went to #path-1??

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

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