-- Creating a sorted list of positions to move between

local positionList = {}
while true do
	local lowest = {nil, math.huge} -- Contains the destination with the lowest number and its number
	for _, value in pairs(mainFolder:GetChildren()) do
		if not value:IsA("BasePart") then continue end -- Skip this item if it's not a base part
		local nameSplit = string.split(value.Name, " ")
		-- In these conditional statements, we check:
		if #nameSplit ~= 2 then continue end -- That the name contains one space
		local destinationNumber = tonumber(nameSplit[2])
		if string.lower(nameSplit[1]) == "destination" -- The word before the space is "destination"
			and destinationNumber -- The word after the space is a number
			and destinationNumber < lowest[2] -- And the number after the space is lower than the current lowest number
		then
			lowest = {value, destinationNumber} -- If the previous checks succeed, the current part is now the lowest one
		end
	end
	if not lowest[1] then
		break -- End the loop if there are no destination blocks remaining
	else
		table.insert(positionList, lowest[1].Position)
		if hideDestinationBlocks then -- Destroy or rename the current lowest destination to continue the sorting process
			lowest[1]:Destroy()
		else
			lowest[1].Name = "Destination"
		end
	end
end

alignPosition.MaxVelocity = topSpeed
alignPosition.Position = positionList[1]
platform.AlignOrientation.CFrame = platform.CFrame.Rotation -- Match the target orientation to the current orientation
platform.Anchored = false

while true do -- The moving platform can now endlessly go through the sorted list in order
	for index, _ in pairs(positionList) do
		alignPosition.Position = positionList[index]
		task.wait(moveDelay)
	end
end
