I am currently using a script in my Roblox guess game and i made a GUI that enables players to skip stages by purchasing a developer product. I’m encountering a challenge where I would like to allow players to return to a previous stage (e.g., from stage 3 to stage 2) by interacting with a checkpoint. Essentially, if a player touches checkpoint 3 and then touches checkpoint 2, their leaderstat for the current stage should change back to stage 2.
If necessary, I can share the script for further clarification.
local dataStoreService = game:GetService("DataStoreService")
local stageStore = dataStoreService:GetDataStore("PlayerStageDataStore")
function plrToStage(plr)
repeat wait() until plr.Character.HumanoidRootPart
local stagePart = game.Workspace.Checkpoints:FindFirstChild(tostring(plr.leaderstats.Stage.Value))
plr.Character.HumanoidRootPart.CFrame = stagePart.CFrame + Vector3.new(0,2.5,0)
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 success, errorMsg = pcall(function()
stage.Value = stageStore:GetAsync(player.UserId)
end)
if success then
print("Successfuully got "..player.Name.."'s stage data.")
else
warn(errorMsg)
end
if player.Character then
plrToStage(player)
end
player.CharacterAdded:Connect(function()
plrToStage(player)
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errorMsg = pcall(function()
stageStore:SetAsync(player.UserId,player.leaderstats.Stage.Value)
end)
if success then
print("Successfuully saved "..player.Name.."'s stage data.")
else
warn(errorMsg)
end
end)
for _, checkpoint in pairs(game.Workspace.Checkpoints:GetChildren()) do
checkpoint.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
local checkpointNumber = tonumber(checkpoint.Name)
if player.leaderstats.Stage.Value < checkpointNumber then
player.leaderstats.Stage.Value = checkpointNumber
end
end
end)
end
So, you want it so the player can go to the previous stage is that it?
If yes, here’s the script:
local dataStoreService = game:GetService("DataStoreService")
local stageStore = dataStoreService:GetDataStore("PlayerStageDataStore")
function plrToStage(plr)
repeat wait() until plr.Character.HumanoidRootPart
local stagePart = game.Workspace.Checkpoints:FindFirstChild(tostring(plr.leaderstats.Stage.Value))
plr.Character.HumanoidRootPart.CFrame = stagePart.CFrame + Vector3.new(0,2.5,0)
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 success, errorMsg = pcall(function()
stage.Value = stageStore:GetAsync(player.UserId)
end)
if success then
print("Successfuully got "..player.Name.."'s stage data.")
else
warn(errorMsg)
end
if player.Character then
plrToStage(player)
end
player.CharacterAdded:Connect(function()
plrToStage(player)
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errorMsg = pcall(function()
stageStore:SetAsync(player.UserId,player.leaderstats.Stage.Value)
end)
if success then
print("Successfuully saved "..player.Name.."'s stage data.")
else
warn(errorMsg)
end
end)
for _, checkpoint in pairs(game.Workspace.Checkpoints:GetChildren()) do
checkpoint.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
local checkpointNumber = tonumber(checkpoint.Name)
if checkpointNumber == player.leaderstats.Stage.Value + 1 or checkpointNumber == player.leaderstats.Stage.Value - 1 then
player.leaderstats.Stage.Value = checkpointNumber
end
end
end)
end
Script works, but not as i indented it to. It does change the stage but it does only in order, it can’t change for example from stage 6 to 2, only in order. I want it to change the stage to the stage the person is currently is, if they go to stage 5 for example and go to spawn which is stage 1, i want it to change from stage 5 to stage 1. Scripts works indeed, but not as i wanted it to. Thanks for any help!
It’s ok. I can still give you the corrected script you prompted me:
local dataStoreService = game:GetService("DataStoreService")
local stageStore = dataStoreService:GetDataStore("PlayerStageDataStore")
function plrToStage(plr)
repeat wait() until plr.Character.HumanoidRootPart
local stagePart = game.Workspace.Checkpoints:FindFirstChild(tostring(plr.leaderstats.Stage.Value))
plr.Character.HumanoidRootPart.CFrame = stagePart.CFrame + Vector3.new(0,2.5,0)
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 success, errorMsg = pcall(function()
stage.Value = stageStore:GetAsync(player.UserId)
end)
if success then
print("Successfuully got "..player.Name.."'s stage data.")
else
warn(errorMsg)
end
if player.Character then
plrToStage(player)
end
player.CharacterAdded:Connect(function()
plrToStage(player)
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errorMsg = pcall(function()
stageStore:SetAsync(player.UserId,player.leaderstats.Stage.Value)
end)
if success then
print("Successfuully saved "..player.Name.."'s stage data.")
else
warn(errorMsg)
end
end)
for _, checkpoint in pairs(game.Workspace.Checkpoints:GetChildren()) do
checkpoint.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
local checkpointNumber = tonumber(checkpoint.Name)
if checkpointNumber == player.leaderstats.Stage.Value + 1 or checkpointNumber < player.leaderstats.Stage.Value then
player.leaderstats.Stage.Value = checkpointNumber
end
end
end)
end
for _, checkpoint in pairs(game.Workspace.Checkpoints:GetChildren()) do
checkpoint.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
local checkpointNumber = tonumber(checkpoint.Name)
if checkpointNumber == player.leaderstats.Stage.Value + 1 then
player.leaderstats.Stage.Value = checkpointNumber
elseif checkpointNumber < player.leaderstats.Stage.Value then
player.leaderstats.Stage.Value == checkpointNumber
end
end
end)
end
Note that formatting is a bit off since I did this all on the devforum but it should work still