Hello guys, so I created an obby where it’s generated randomly for each server but the thing is I want the user’s checkpoint to save using this script so whenever they leave the game and join a different server you’ll be teleported back to your current level.
Unfortunately the problem with this script is that it doesn’t work on new servers because there’s no children on “checkpoints” since the new servers will have to generate the checkpoints first and then add it to the folder. The script doesn’t detect the new checkpoints being added to the folder.
How do I make it so that the script will also detect the new checkpoints being added to the folder? and how do I also add so that when the user datastore is currently at a higher level but the checkpoints haven’t loaded there yet, the game will make you wait for the checkpoints to be completely added to the game via a loading screen before you spawn in.
I’m not an experience programmer myself and I frequently struggle with the basics sometimes, I would highly appreciate any response or help. Thank you!
Here’s the full script for details:
----- Variables -----
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")
local ObbyDataStore = DataStoreService:GetDataStore("DataCheckpointTester") -- Test Data Store, Not official Data
local checkpoints = game.Workspace.Checkpoints -- Checkpoint Folder-
local FIRST_CHECKPOINT = 1
----- Events -----
Players.PlayerAdded:Connect(function(player)
local playerKey = "ID_" .. player.UserId
local retrievedData
local success, errorMessage = pcall(function()
retrievedData = ObbyDataStore:GetAsync(playerKey)
end)
if not success and not RunService:IsStudio() then
player:Kick("Error loading data, please rejoin.")
return
end
if not retrievedData then
retrievedData = {}
end
-- Create leaderstats
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
-- Create stage value
local stageValue = Instance.new("IntValue")
stageValue.Name = "Stage"
stageValue.Value = retrievedData.Stage or FIRST_CHECKPOINT
stageValue.Parent = leaderstats
player.CharacterAdded:Connect(function(character)
wait() -- Wait for the character to load
local checkpoint = checkpoints:FindFirstChild(tostring(stageValue.Value))
if checkpoint then
character:WaitForChild("HumanoidRootPart").CFrame = checkpoint.CFrame + Vector3.new(0, 3 + (checkpoint.Size.Y / 2), 0)
end
end)
end)
for i, v in pairs(checkpoints:GetChildren()) do
v.Touched:Connect(function(hit)
local player = Players:GetPlayerFromCharacter(hit.Parent)
if not player or not player:FindFirstChild("leaderstats") then
return
end
if player.leaderstats.Stage.Value == tonumber(v.Name) - 1 then -- Prevents the player from skipping checkpoints and going backward
player.leaderstats.Stage.Value = tonumber(v.Name)
end
end)
end
game.Players.PlayerRemoving:Connect(function(player)
if not player:FindFirstChild("leaderstats") then
return
end
local playerKey = "ID_" .. player.UserId
local stage = player.leaderstats.Stage.Value
local success, errorMessage = pcall(function()
ObbyDataStore:SetAsync(playerKey, {Stage = stage})
end)
if not success and not RunService:IsStudio() then
warn("Error while saving player data")
end
end)