I have a function here that checks the distances between waypoints in a waypoints folder. For some reason, the i value randomly changes midway through the function when it shouldn’t be. what should be “Distance of waypoint 10 to 9” and so on is instead a random number each time. As you can see in the screenshot, some of them work just fine, but then some numbers just break normality. Is there a fix to this?

for i=#waypoints, 1, -1 do
wait()
print(i)
if waypoints[i-1] then
local Distance = (waypoints[i].Position - waypoints[i-1].Position).Magnitude
TotalDistance = TotalDistance + Distance
print("Distance of waypoint " .. waypoints[i].Name .. " to " .. waypoints[i-1].Name .. " : " .. Distance)
end
end