Obby stage select code not working

I am attempting to make a stage selector for my dco, but it isn’t working. The buttons “Forward” and “Back” are meant to make me teleport to the next stage, or previous stage (only if you have already beaten that stage). The buttons “vForward” and “vBack” are meant to teleport me 10 stages forward, or 10 stages backward (only if you have already beaten those stages). The buttons “Forward” and “Back” are just teleporting me to random stages, though. The button “vForward” always teleports me to the max stages i’ve reached, and the button “vBack” always teleports me to stage 0.

Here is my code:

local st = script.Parent
local plr = game.Players.LocalPlayer
local cp = workspace.Checkpoints

plr:WaitForChild("TeleportedStage")
repeat task.wait() until plr.leaderstats.Stage.Value ~= 0
plr.TeleportedStage.Value = plr.leaderstats.Stage.Value

while task.wait() do
	st.CurrentStage.Text = tostring(plr.TeleportedStage.Value)

	st.Back.MouseButton1Down:Connect(function()
		if plr.TeleportedStage.Value == 0 then
			plr.TeleportedStage.Value = plr.leaderstats.Stage.Value
			plr.Character.HumanoidRootPart.CFrame = CFrame.new(cp[plr.leaderstats.Stage.Value].Position)
		else
			plr.TeleportedStage.Value -= 1
			plr.Character.HumanoidRootPart.CFrame = CFrame.new(cp[plr.TeleportedStage.Value].Position)
		end
	end)

	st.vBack.MouseButton1Down:Connect(function()
		if plr.TeleportedStage.Value < 10 then
			plr.TeleportedStage.Value = 0
			plr.Character.HumanoidRootPart.CFrame = CFrame.new(cp["0"].Position)
		else
			plr.TeleportedStage.Value -= 10
			plr.Character.HumanoidRootPart.CFrame = CFrame.new(cp[plr.TeleportedStage.Value].Position)
		end
	end)

	st.Forward.MouseButton1Down:Connect(function()
		if plr.TeleportedStage.Value == plr.leaderstats.Stage.Value then
			plr.TeleportedStage.Value = 0
			plr.Character.HumanoidRootPart.CFrame = CFrame.new(cp["0"].Position)
		else
			plr.TeleportedStage.Value += 1
			plr.Character.HumanoidRootPart.CFrame = CFrame.new(cp[plr.TeleportedStage.Value].Position)
		end
	end)

	st.vForward.MouseButton1Down:Connect(function()
		plr.TeleportedStage.Value += 10
		if plr.TeleportedStage.Value > plr.leaderstats.Stage.Value then
			plr.TeleportedStage.Value = plr.leaderstats.Stage.Value
			plr.Character.HumanoidRootPart.CFrame = CFrame.new(cp[plr.leaderstats.Stage.Value].Position)
		else
			
			plr.Character.HumanoidRootPart.CFrame = CFrame.new(cp[plr.TeleportedStage.Value].Position)
		end
	end)
end
3 Likes

I should probably also mention that it continues to get laggier as I play the game, what I mean by this is that when I click one of the buttons when I first join, it teleports me (to the wrong checkpoint of course) really quick, but as I continue to play, it takes longer and longer for it to respond when I click the button.

1 Like

The lag and problem are caused by the fact that your code is running in a while loop. So what’s happening is every frame a new function is added to the event, so when the event eventually fires all those functions will execute. This makes it so when you click the button, it’s trying to teleport you maybe 1000 stages. But lucky for you, you made it so you only teleport to the first stage if there aren’t 10 levels in front or behind causing you to teleport to the first stage or last stage. The lag is caused by the amount of memory it takes to store all those functions.

The solution is pretty simple: just move the connections outside the while loop.

3 Likes

I moved the functions outside the while loop like you said, but now, when I click the buttons, nothing happens. The script is in StarterGui if that helps or changes anything. I can post my new script here if you would like.

2 Likes

Can you post your new script, also any errors in the output? Also, did you move the functions above the while loop or below? If below, then just move them above it.

3 Likes

Thank you, I accidently put them below the while loop. I do have one more question if you don’t mind answering. When the player is teleported, you are teleported inside the checkpoint, and are then launched up, sometimes you even fall through the ground. Is there a way to make it so you spawn above the checkpoint instead of inside it?

2 Likes

If possible, you can move the part you teleport to a bit above, else you can just add an additional offset like this:

cp[plr.TeleportedStage.Value].Position + Vector3.new(0, 2, 0)
2 Likes