GUI teleport is not working properly

Hello! I have a problem. I want to teleport through the levels using the arrows. But if the player is at the highest level, he must go to 1 by pressing the forward arrow. And from 1 he should return to the highest by pressing the back arrow.

Actually, my problem is that this system only works on the numbers, while the player himself does not teleport to the end or the beginning.

I understand where the error is, but my programming knowledge is not enough to fix it.

here is the script. (the second half of the script is incorrect)

game.Workspace:WaitForChild(game.Players.LocalPlayer.Name)

repeat wait() until game.Players.LocalPlayer.Character

repeat wait() until game.Players.LocalPlayer.stage
wait()

local Players = game:GetService("Players")

local plr = game.Players.LocalPlayer
local plrName = plr.Name

if plr.stage.Value > #game.Workspace.Checkpoints:GetChildren() then
	local currentStageNumber = plr.stage.Value
	local currentCheckpoint = game.Workspace.Checkpoints:FindFirstChild(tostring(#game.Workspace.Checkpoints:GetChildren()))

	local currentCheckpointPos = currentCheckpoint.Position
	local X = currentCheckpointPos.X
	local Y = currentCheckpointPos.Y
	local Z = currentCheckpointPos.Z
	local TPPlace = CFrame.new(X, Y+5, Z)

	plr.Character.Torso.CFrame = TPPlace
else
	local currentStageNumber = plr.leaderstats.Stage.Value
	local currentCheckpoint = game.Workspace.Checkpoints:FindFirstChild(tostring(currentStageNumber))

	local currentCheckpointPos = currentCheckpoint.Position
	local X = currentCheckpointPos.X
	local Y = currentCheckpointPos.Y
	local Z = currentCheckpointPos.Z
	local TPPlace = CFrame.new(X, Y+5, Z)

	plr.Character.Torso.CFrame = TPPlace
end

(And let me be honest. the script is not mine. I took it from another model. however, it did not fully fit my previous scripts and I had to modify it a little. the original script states that you cannot move further from the maximum level. hence the history of the error.)

You may be wondering what it looks like and what I want.

3 Likes

I’d like to know when the code runs. Do you run it right after the player changes the number?

Yes, the action starts when the player clicks the arrow.

So assuming that you’re changing plr.stage.Value, what you want to do is wrap it around before you get the checkpoint to teleport the player to:

local checkpoints = game.Workspace.Checkpoints
local checkpointCount = #checkpoints:GetChildren()
if plr.stage.Value < 1 then 
    plr.stage.Value = checkpointCount
elseif plr.stage.Value > checkpointCount then
    plr.stage.Value = 1
end

local currentCheckpoint = checkpoints:FindFirstChild(tostring(plr.stage.Value))

local TPPlace = CFrame.new(currentCheckpoint.Position + Vector3.new(0,5,0))
plr.Character.Torso.CFrame = TPPlace

error: attempt to index nil with ‘Position’ (13)

1 Like

I don’t exactly know how your checkpoints are named, but try doing

print(checkpoints:GetChildren())
print(plr.stage.Value)

before you assign currentCheckpoint, and send a screenshot of what it shows, and I’ll try to figure something out from there.


If you asked about it, then these are just numbers.

1 Like

Oh, it’s because there’s an extra checkpoint for spawn, so it thinks there are 5 checkpoints. You can simply just change the assignment for checkpointCount to:

local checkpointCount = #checkpoints:GetChildren() - 1

The error has been fixed, but the player is still not teleporting from the fourth level to the first one. Perhaps the issue is not with this script? (There are a total of four scripts involved in this process.)

1 Like

What’s the code that changes the TextLabel? I noticed that it doesn’t go beyond 1 and 4.

there are several of them.

game.Players.PlayerAdded:Connect(function(plr)
	repeat wait() until plr.leaderstats
	local Stage = Instance.new("IntValue")
	Stage.Parent = plr
	Stage.Name = "stage"
	Stage.Value = plr.leaderstats.Stage.Value
	print(Stage.Value)
end)
game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local stage = Instance. new("IntValue")
	stage.Name = "Stage"
    stage.Parent = leaderstats
local plr = game.Players.LocalPlayer
local plrName = plr.Name
local plrChar = game.Workspace:WaitForChild(tostring(plrName))

local maxStage = #game.Workspace.Checkpoints:GetChildren() -- Number of stages.
print(tostring(maxStage).." Stages.")

local frame = script.Parent

local plr = game.Players.LocalPlayer


function charStage()
	local currentStageNumber = plr.stage.Value
	local currentCheckpoint = game.Workspace.Checkpoints:FindFirstChild(tostring(currentStageNumber))
	print(tostring(currentCheckpoint.Name).." -Current Checkpoint")
	local currentCheckpointPos = currentCheckpoint.Position
	local X = currentCheckpointPos.X
	local Y = currentCheckpointPos.Y
	local Z = currentCheckpointPos.Z
	local TPPlace = CFrame.new(X, Y+5, Z)
	plr.Character.Torso.CFrame = TPPlace
end
	

frame.B1.MouseButton1Down:Connect(function()
	if plr.stage.Value - 1 > 0 then
		plr.stage.Value = plr.stage.Value - 1
		charStage()
	else
		plr.stage.Value = plr.leaderstats.Stage.Value
	end
end)

frame.B10.MouseButton1Down:Connect(function()
	if plr.stage.Value - 10 > 0 then
		plr.stage.Value = plr.stage.Value - 10
		charStage()
	else
		plr.stage.Value = plr.leaderstats.Stage.Value
	end
end)

frame.F1.MouseButton1Down:Connect(function()
	if plr.stage.Value + 1 <= maxStage and plr.stage.Value + 1 <= plr.leaderstats.Stage.Value then
		plr.stage.Value = plr.stage.Value + 1
		charStage()
	else
		plr.stage.Value = 1
	end
end)


frame.F10.MouseButton1Down:Connect(function()
	if plr.stage.Value + 10 <= maxStage and plr.stage.Value + 10 <= plr.leaderstats.Stage.Value then
		plr.stage.Value = plr.stage.Value + 10
		charStage()
	else
		plr.stage.Value = 1
	end
end)

tweenService = game:GetService("TweenService")

repeat wait() until game.Players.LocalPlayer.stage


game.Players.LocalPlayer.stage.Value = game.Players.LocalPlayer.leaderstats.Stage.Value

script.Parent.Text = tostring(game.Players.LocalPlayer.stage.Value)

game.Players.LocalPlayer.leaderstats.Stage.Changed:Connect(function()
	game.Players.LocalPlayer.stage.Value = game.Players.LocalPlayer.leaderstats.Stage.Value
	script.Parent.Text = tostring(game.Players.LocalPlayer.stage.Value)
	script.Parent.Sound:Play()
end)

game.Players.LocalPlayer.stage.Changed:Connect(function()
	script.Parent.Text = tostring(game.Players.LocalPlayer.stage.Value)
end)

That 3rd script caught my eye.
The code that handles the Forward 1 button doesn’t actually run if the stage number is already at the maximum. You can simply just remove that condition and it should work fine.
Infact, all 4 button handlers can simply just be reduced to just 2 lines each, since the code to teleport the player will handle the rest:

frame.B1.MouseButton1Down:Connect(function()
    plr.stage.Value -= 1
    charStage()
end)

frame.B10.MouseButton1Down:Connect(function()
    plr.stage.Value -= 10
    charStage()
end)

frame.F1.MouseButton1Down:Connect(function()
    plr.stage.Value += 1
    charStage()
end)

frame.F10.MouseButton1Down:Connect(function()
    plr.stage.Value += 10
    charStage()
end)

It is necessary to specify the maximum number, since players cannot (should not) press the “Forward” button until they complete this stage.
in addition, starting from stage 4, they do not return to 1, but, on the contrary, move on to 5. (which does not exist)

1 Like

That’s my fault. In that case, wrap the value back to 1 if the new stage.Value is higher than the leaderstats value:

frame.F1.MouseButton1Down:Connect(function()
    plr.stage.Value += 1
    if plr.stage.Value > plr.leaderstats.Stage.Value then
        plr.stage.Value = 1
    end
    charStage()
end)

frame.F10.MouseButton1Down:Connect(function()
    plr.stage.Value += 10
    if plr.stage.Value > plr.leaderstats.Stage.Value then
        plr.stage.Value = 1
    end
    charStage()
end)

I’m a little confused on what causes the 2nd issue. Hopefully it’s somehow fixed by the above code.

THANK YOU SO MUCH!
it really helped :slight_smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.