Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local DataStoreService = game:GetService("DataStoreService")
- local leaderstatsDataStore = DataStoreService:GetDataStore("ObbyLeaderstats")
- local STAT_NAME = "Stage"
- local PREVENT_SKIPPING = true
- local checkpoints = {}
- local i = 1
- while true do
- local checkpoint = game.Workspace:FindFirstChild("Checkpoint " .. i, true)
- if not checkpoint then
- print("Last Checkpoint : " .. i - 1)
- break
- end
- table.insert(checkpoints, checkpoint)
- i = i + 1
- end
- game.Players.PlayerAdded:Connect(function(player)
- -- Create leaderstats model
- local leaderstats = Instance.new("Model")
- leaderstats.Name = "leaderstats"
- leaderstats.Parent = player
- -- Create the Stage stat
- local checkpointStat = Instance.new("IntValue")
- checkpointStat.Name = STAT_NAME
- checkpointStat.Value = 1
- checkpointStat.Parent = leaderstats
- -- Attempt to load saved data
- local success, data = pcall(function()
- return leaderstatsDataStore:GetAsync("Player_" .. player.UserId)
- end)
- if success and data then
- checkpointStat.Value = data.Stage or 1
- else
- if not success then
- warn("Failed to load data for player " .. player.Name)
- end
- end
- player.CharacterAdded:Connect(function(character)
- local goto = checkpoints[checkpointStat.Value]
- if goto then
- repeat wait() until character.Parent
- character:MoveTo(goto.Position)
- else
- warn("Checkpoint " .. checkpointStat.Value .. " not found")
- end
- end)
- end)
- game.Players.PlayerRemoving:Connect(function(player)
- local leaderstats = player:FindFirstChild("leaderstats")
- if leaderstats then
- local stage = leaderstats:FindFirstChild(STAT_NAME)
- local dataToSave = {
- Stage = stage and stage.Value or 1,
- }
- local success, err = pcall(function()
- leaderstatsDataStore:SetAsync("Player_" .. player.UserId, dataToSave)
- end)
- if not success then
- warn("Failed to save data for player " .. player.Name .. ": " .. err)
- end
- end
- end)
- for index, checkpoint in ipairs(checkpoints) do
- checkpoint.Touched:Connect(function(hit)
- local player = game.Players:GetPlayerFromCharacter(hit.Parent)
- if not player then return end
- local humanoid = hit.Parent:FindFirstChild("Humanoid")
- if not humanoid or humanoid.Health <= 0 then return end
- local leaderstats = player:FindFirstChild("leaderstats")
- if not leaderstats then return end
- local checkpointStat = leaderstats:FindFirstChild(STAT_NAME)
- if not checkpointStat then return end
- if (PREVENT_SKIPPING and checkpointStat.Value + 1 == index) or (not PREVENT_SKIPPING and checkpointStat.Value < index) then
- checkpointStat.Value = index
- end
- end)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement