Why is this only teleporting me to the first position?

I have a problem I have a checkpoint folder in a race and an intvalue that tells me how many checkpoints there are but my code only is teleporting me to the first checkpoint?

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer

for Number = 1, workspace.Checkpoints.NumberOfCheckpoints.Value do
	pcall(function()
		LocalPlayer.Character:MoveTo(workspace.Checkpoints[Number].Position)
	end)
	wait()
end

BTW am adding another checkpoint for every checkpoint they go thrugh so at the start there is one once they go throigh num 1 I add num 6 num 2 num 7 etc

but it just keeps teleporting them to the first checkpoint idk why

AFTER A WHILE THE SCRIPT IS CRASHING MY GAME

would you want to have you checkpoints as part inside the same parent and do :getchildren()? if so you could do

local Players = game:GetService(“Players”)
local LocalPlayer = Players.LocalPlayer

teleportcheckpoint() --the parameter is the one we want to teleport to eg: teleportcheckpoint(1)

function teleportcheckpoint(num) checkpoint = table.find(workspace.Checkpoints:getchildren(), “checkpoint”…num)
LocalPlayer.Character:MoveTo(checkpoint.position)
end)
wait()
end

This is assuming that the checkpoints are parts named checkpoint1, 2, etc and that they have a .position

sorry If I made any mistakes, im on my phone

you could just do

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()

for i,v in pairs(workspace.Checkpoints:GetChildren()) do
   if v:IsA("Part") then
        Character:MoveTo(v.Position)
        wait(1)
   end

end

yes but my script now works but it is crashing me after a while

yes but my script now works but it is crashing me after a while

which script are you using? can you post the exact code.

while true do
	local Checkpoints = workspace:FindFirstChild("Checkpoints")
	if Checkpoints then
		for Number = 1, Checkpoints.NumberOfCheckpoints.Value do
			pcall(function()
				LocalPlayer:MoveTo(Checkpoints[Number].Position)
			end)
			wait()
		end
	else
		warn("Checkpoints not loaded!")
	end
end

AND IT JUST CRAHSES AFTER ABOUT 1 MIN AND I DONT KNOW WHy

you are doing while true without a hard wait() try putting wait() right after while true

I have a wait in the while true do though

is it successfully teleporting you, if it is, it is most likely eating your memory causing a crash, if it isn’t teleporting then that means the wait() you have is being bypassed meaning it is an infinite loop with no restraints

This is ultimately what you should do:

local Checkpoints = workspace:WaitForChild("Checkpoints")
for _, checkpoint in pairs(Checkpoints:GetChildren()) do
    LocalPlayer.Character:MoveTo(checkpoint.Position)
end

That should fix the crashing, wait for the checkpoints to exist and that should teleport you to every checkpoint.

It crashes after a mintiute or so though not straight away

If you wouldn’t mind, can you explain the usage of the while loop? using a while loop is resource intensive, and should only be used if required

Maybe you could try instead:

for _, checkpoint in pairs(Checkpoints:GetChildren()) do
    LocalPlayer.Character:MoveTo(checkpoint.Position)
    LocalPlayer.Character.Humanoid.MoveToFinished:Wait() -- Waits until the player finishes moving.
--Script then moves to the next checkpoint after this. This works with a while true loop.
end

An issue with your script was the fact that it didn’t wait for the player to finish moving to the first checkpoint.