I have an issue where when I go back a stage and I die, the GUI resets to the one I was previously at as you can see shown below.
Here is the GUI handler script and the datastore scripts…
Datastore Script:
local DataService = game:GetService("DataStoreService")
local DataStore = DataService:GetDataStore("data_01")
local camera = workspace.CurrentCamera
local cpFolder = game.Workspace:WaitForChild("Checkpoints")
game.Players.PlayerAdded:Connect(function(player)
local key = "player-" .. player.UserId
local GetSave = DataStore:GetAsync(key) -- check for existing data
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local hidden = Instance.new("Folder", player)
hidden.Name = "hidden"
local checkpoint = Instance.new("IntValue", leaderstats)
checkpoint.Name = "Stage"
local spawnpoint = Instance.new("IntValue", hidden)
spawnpoint.Name = "SpawnPoint"
if GetSave then
checkpoint.Value = GetSave
print("Data Loaded For " .. player.Name)
else
checkpoint.Value = 1
print("New Data Created For " .. player.Name)
end
player.hidden.SpawnPoint.Value = player.leaderstats.Stage.Value
local character = player.Character or player.CharacterAdded:Wait()
local respawn2 = cpFolder:WaitForChild(player.leaderstats.Stage.Value)
character:WaitForChild("HumanoidRootPart").CFrame = respawn2.CFrame + Vector3.new(0,3,0)
player.CharacterAdded:Connect(function(character)
repeat wait() until workspace:FindFirstChild(character.Name)
local player = game.Players:GetPlayerFromCharacter(character)
local respawn = cpFolder[player.hidden.SpawnPoint.Value]
character.HumanoidRootPart.CFrame = respawn.CFrame + Vector3.new(0,3,0)
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
local key = "player-" .. player.UserId
DataStore:SetAsync(key, player.leaderstats.Stage.Value)
print("Data Successfully Saved For " .. player.Name)
end)
GUI Handler Script:
local UI = script.Parent
local player = game.Players.LocalPlayer
local checkpoints = workspace:WaitForChild("Checkpoints")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Teleport = ReplicatedStorage:WaitForChild("Teleport")
local Counter = UI:WaitForChild("Counter")
local Level = Counter:WaitForChild("Level")
local Previous = UI:WaitForChild("Previous")
local Next = UI:WaitForChild("Next")
local Previous1 = UI:WaitForChild("Previous1")
local Next1 = UI:WaitForChild("Next1")
local CurrentValue = UI:WaitForChild("Current")
local function previousLevel()
script.Parent.Handler.Click:Play()
if CurrentValue.Value ~= 1 then
CurrentValue.Value = CurrentValue.Value - 1
player.hidden.SpawnPoint.Value = player.hidden.SpawnPoint.Value - 1
print("Wait")
Level.Text = CurrentValue.Value
-- teleport
Teleport:FireServer(checkpoints:FindFirstChild(tostring(CurrentValue.Value)))
end
end
local function previousLevel2()
script.Parent.Handler.Click:Play()
if CurrentValue.Value >= 11 then
CurrentValue.Value = CurrentValue.Value - 10
player.hidden.SpawnPoint.Value = player.hidden.SpawnPoint.Value - 10
Level.Text = CurrentValue.Value
-- teleport
Teleport:FireServer(checkpoints:FindFirstChild(tostring(CurrentValue.Value)))
end
end
local function nextLevel()
script.Parent.Handler.Click:Play()
if (CurrentValue.Value + 1) <= player.leaderstats.Stage.Value then
CurrentValue.Value = CurrentValue.Value + 1
player.hidden.SpawnPoint.Value = player.hidden.SpawnPoint.Value + 1
Level.Text = CurrentValue.Value
-- teleport
Teleport:FireServer(checkpoints:FindFirstChild(tostring(CurrentValue.Value)))
end
end
local function nextLevel2()
script.Parent.Handler.Click:Play()
if (CurrentValue.Value + 10) <= player.leaderstats.Stage.Value then
CurrentValue.Value = CurrentValue.Value + 10
player.hidden.SpawnPoint.Value = player.hidden.SpawnPoint.Value + 10
Level.Text = CurrentValue.Value
-- teleport
Teleport:FireServer(checkpoints:FindFirstChild(tostring(CurrentValue.Value)))
end
end
Previous.MouseButton1Click:Connect(function()
previousLevel()
end)
Previous1.MouseButton1Click:Connect(function()
previousLevel2()
end)
Next.MouseButton1Click:Connect(function()
nextLevel()
end)
Next1.MouseButton1Click:Connect(function()
nextLevel2()
end)
CurrentValue.Value = player:WaitForChild("leaderstats").Stage.Value
Level.Text = CurrentValue.Value
player.leaderstats.Stage:GetPropertyChangedSignal("Value"):Connect(function()
CurrentValue.Value = player.leaderstats.Stage.Value
Level.Text = CurrentValue.Value
end)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Teleport = ReplicatedStorage:WaitForChild("Teleport")
Teleport.OnServerEvent:Connect(function(player, checkpoint)
local character = player.Character
if character then
character.HumanoidRootPart.CFrame = checkpoint.CFrame + Vector3.new(0,2,0)
end
end)
This is where you handle the stage value, so when the player spawns it will not comeback to the old stage.
To make a server-sided level script maybe create new Remote Event to handle the change level script.
This is my own code for that:
-- You can modify because this is only an example.
local RS = game:GetService("ReplicatedStorage")
local Events = RS:WaitForChild("Events Folder")
local levelChanger = Events:WaitForChild("ChangeLevel")
levelChanger.OnServerEvent:Connect(function(sender, stage)
if sender then
sender.hidden.SpawnPoint.Value = stage
end
end)
Deleted the folder and just put the remote event inside ReplicatedStorage but it still won’t work.
local RS = game:GetService("ReplicatedStorage")
local levelChanger = RS:WaitForChild("ChangeLevel")
levelChanger.OnServerEvent:Connect(function(sender, stage)
if sender then
sender.hidden.SpawnPoint.Value = stage
end
end)
local UI = script.Parent
local player = game.Players.LocalPlayer
local checkpoints = workspace:WaitForChild("Checkpoints")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ChangeLevel = ReplicatedStorage:WaitForChild("ChangeLevel")
local Counter = UI:WaitForChild("Counter")
local Level = Counter:WaitForChild("Level")
local Previous = UI:WaitForChild("Previous")
local Next = UI:WaitForChild("Next")
local Previous1 = UI:WaitForChild("Previous1")
local Next1 = UI:WaitForChild("Next1")
local CurrentValue = UI:WaitForChild("Current")
local function previousLevel()
script.Parent.Handler.Click:Play()
if CurrentValue.Value ~= 1 then
CurrentValue.Value = CurrentValue.Value - 1
player.hidden.SpawnPoint.Value = player.hidden.SpawnPoint.Value - 1
print("Wait")
Level.Text = CurrentValue.Value
-- teleport
ChangeLevel:FireServer(checkpoints:FindFirstChild(tostring(CurrentValue.Value)))
end
end
local function previousLevel2()
script.Parent.Handler.Click:Play()
if CurrentValue.Value >= 11 then
CurrentValue.Value = CurrentValue.Value - 10
player.hidden.SpawnPoint.Value = player.hidden.SpawnPoint.Value - 10
Level.Text = CurrentValue.Value
-- teleport
ChangeLevel:FireServer(checkpoints:FindFirstChild(tostring(CurrentValue.Value)))
end
end
local function nextLevel()
script.Parent.Handler.Click:Play()
if (CurrentValue.Value + 1) <= player.leaderstats.Stage.Value then
CurrentValue.Value = CurrentValue.Value + 1
player.hidden.SpawnPoint.Value = player.hidden.SpawnPoint.Value + 1
Level.Text = CurrentValue.Value
-- teleport
ChangeLevel:FireServer(checkpoints:FindFirstChild(tostring(CurrentValue.Value)))
end
end
local function nextLevel2()
script.Parent.Handler.Click:Play()
if (CurrentValue.Value + 10) <= player.leaderstats.Stage.Value then
CurrentValue.Value = CurrentValue.Value + 10
player.hidden.SpawnPoint.Value = player.hidden.SpawnPoint.Value + 10
Level.Text = CurrentValue.Value
-- teleport
ChangeLevel:FireServer(checkpoints:FindFirstChild(tostring(CurrentValue.Value)))
end
end
Previous.MouseButton1Click:Connect(function()
previousLevel()
end)
Previous1.MouseButton1Click:Connect(function()
previousLevel2()
end)
Next.MouseButton1Click:Connect(function()
nextLevel()
end)
Next1.MouseButton1Click:Connect(function()
nextLevel2()
end)
CurrentValue.Value = player:WaitForChild("leaderstats").Stage.Value
Level.Text = CurrentValue.Value
player.leaderstats.Stage:GetPropertyChangedSignal("Value"):Connect(function()
CurrentValue.Value = player.leaderstats.Stage.Value
Level.Text = CurrentValue.Value
end)
Put back the Teleport remote event because it handles the teleport script the ChangeLevel remote only handles the Level value.
To sum up all of this to shorten this conversation I fixed your code.
GUI Handler Script:
local UI = script.Parent
local player = game.Players.LocalPlayer
local checkpoints = workspace:WaitForChild("Checkpoints")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ChangeLevel = ReplicatedStorage:WaitForChild("ChangeLevel")
local Teleport = ReplicatedStorage:WaitForChild("Teleport")
local Counter = UI:WaitForChild("Counter")
local Level = Counter:WaitForChild("Level")
local Previous = UI:WaitForChild("Previous")
local Next = UI:WaitForChild("Next")
local Previous1 = UI:WaitForChild("Previous1")
local Next1 = UI:WaitForChild("Next1")
local CurrentValue = UI:WaitForChild("Current")
local function previousLevel()
script.Parent.Handler.Click:Play()
if CurrentValue.Value ~= 1 then
CurrentValue.Value = CurrentValue.Value - 1
print("Wait")
Level.Text = CurrentValue.Value
-- teleport
Teleport:FireServer(checkpoints:FindFirstChild(tostring(CurrentValue.Value)))
-- Change Level
local changeType = "previous-level-1" --This is only the id to identify in the OnServerEvent
ChangeLevel:FireServer(changeType)
end
end
local function previousLevel2()
script.Parent.Handler.Click:Play()
if CurrentValue.Value >= 11 then
CurrentValue.Value = CurrentValue.Value - 10
Level.Text = CurrentValue.Value
-- teleport
Teleport:FireServer(checkpoints:FindFirstChild(tostring(CurrentValue.Value)))
-- Change Level
local changeType = "previous-level-2" --This is only the id to identify in the OnServerEvent
ChangeLevel:FireServer(changeType)
end
end
local function nextLevel()
script.Parent.Handler.Click:Play()
if (CurrentValue.Value + 1) <= player.leaderstats.Stage.Value then
CurrentValue.Value = CurrentValue.Value + 1
Level.Text = CurrentValue.Value
-- teleport
Teleport:FireServer(checkpoints:FindFirstChild(tostring(CurrentValue.Value)))
-- Change Level
local changeType = "next-level-1" --This is only the id to identify in the OnServerEvent
ChangeLevel:FireServer(changeType)
end
end
local function nextLevel2()
script.Parent.Handler.Click:Play()
if (CurrentValue.Value + 10) <= player.leaderstats.Stage.Value then
CurrentValue.Value = CurrentValue.Value + 10
Level.Text = CurrentValue.Value
-- teleport
Teleport:FireServer(checkpoints:FindFirstChild(tostring(CurrentValue.Value)))
-- Change Level
local changeType = "next-level-2" --This is only the id to identify in the OnServerEvent
ChangeLevel:FireServer(changeType)
end
end
Previous.MouseButton1Click:Connect(function()
previousLevel()
end)
Previous1.MouseButton1Click:Connect(function()
previousLevel2()
end)
Next.MouseButton1Click:Connect(function()
nextLevel()
end)
ChangeLevel Handler Script:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ChangeLevel = ReplicatedStorage:WaitForChild("ChangeLevel")
ChangeLevel.OnServerEvent:Connect(function(sender, changeType)
if changeType == "previous-level-1" then
sender.hidden.SpawnPoint.Value = sender.hidden.SpawnPoint.Value - 1
elseif changeType == "previous-level-2" then
sender.hidden.SpawnPoint.Value = sender.hidden.SpawnPoint.Value - 10
elseif changeType == "next-level-1" then
sender.hidden.SpawnPoint.Value = sender.hidden.SpawnPoint.Value + 1
elseif changeType == "next-level-2" then
sender.hidden.SpawnPoint.Value = sender.hidden.SpawnPoint.Value + 10
else
warn(changeType.." is not recognized as valid id to change level.")
end
end)
This script can be modified in more reliable way, this is only a simple made but it works perfectly.