The problem is that when the player wants to teleport back(if possible), he only moves one position back, even if he can move further.
Visualization
This can be imagined as if the player had 10 cells, but he could only move 1 back, although there are still free cells.
P.S. sortedTable is a table with position storage in Vector3 data format.
local lastCheckpointIndex:number=0
table.sort(sortedTable,function(check1:Vector3,check2:Vector3)
return check1.X>check2.X
end)
for i=1,#sortedTable do
if i>lastCheckpointIndex then
character:PivotTo(CFrame.new(sortedTable[i]+Vector3.new(0,6,0)))
lastCheckpointIndex=i
break
end
end
lastCheckpointIndex=1
it only moves one step back due to the break statement inside the loop. This causes the loop to break after the first checkpoint is found:
for i = #sortedTable, 1, -1 do
if i < lastCheckpointIndex then
character:PivotTo(CFrame.new(sortedTable[i] + Vector3.new(0, 6, 0)))
lastCheckpointIndex = i
break
end
end
I have already tried this solution, it did not work correctly. In this solution, the player is immediately teleported to the end point and can no longer teleport back.