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.
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
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.)
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)
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:
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)
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.