Advertisement
luchitasin9

CheckpointServer Script

Feb 23rd, 2025
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.58 KB | Source Code | 0 0
  1. local DataStoreService = game:GetService("DataStoreService")
  2. local leaderstatsDataStore = DataStoreService:GetDataStore("ObbyLeaderstats")
  3.  
  4. local STAT_NAME = "Stage"
  5. local PREVENT_SKIPPING = true
  6.  
  7. local checkpoints = {}
  8.  
  9. local i = 1
  10. while true do
  11.     local checkpoint = game.Workspace:FindFirstChild("Checkpoint " .. i, true)
  12.     if not checkpoint then
  13.         print("Last Checkpoint : " .. i - 1)
  14.         break
  15.     end
  16.     table.insert(checkpoints, checkpoint)
  17.     i = i + 1
  18. end
  19.  
  20. game.Players.PlayerAdded:Connect(function(player)
  21.     -- Create leaderstats model
  22.     local leaderstats = Instance.new("Model")
  23.     leaderstats.Name = "leaderstats"
  24.     leaderstats.Parent = player
  25.  
  26.     -- Create the Stage stat
  27.     local checkpointStat = Instance.new("IntValue")
  28.     checkpointStat.Name = STAT_NAME
  29.     checkpointStat.Value = 1
  30.     checkpointStat.Parent = leaderstats
  31.  
  32.     -- Attempt to load saved data
  33.     local success, data = pcall(function()
  34.         return leaderstatsDataStore:GetAsync("Player_" .. player.UserId)
  35.     end)
  36.     if success and data then
  37.         checkpointStat.Value = data.Stage or 1
  38.     else
  39.         if not success then
  40.             warn("Failed to load data for player " .. player.Name)
  41.         end
  42.     end
  43.  
  44.     player.CharacterAdded:Connect(function(character)
  45.         local goto = checkpoints[checkpointStat.Value]
  46.         if goto then
  47.             repeat wait() until character.Parent
  48.             character:MoveTo(goto.Position)
  49.         else
  50.             warn("Checkpoint " .. checkpointStat.Value .. " not found")
  51.         end
  52.     end)
  53. end)
  54.  
  55. game.Players.PlayerRemoving:Connect(function(player)
  56.     local leaderstats = player:FindFirstChild("leaderstats")
  57.     if leaderstats then
  58.         local stage = leaderstats:FindFirstChild(STAT_NAME)
  59.         local dataToSave = {
  60.             Stage = stage and stage.Value or 1,
  61.         }
  62.         local success, err = pcall(function()
  63.             leaderstatsDataStore:SetAsync("Player_" .. player.UserId, dataToSave)
  64.         end)
  65.         if not success then
  66.             warn("Failed to save data for player " .. player.Name .. ": " .. err)
  67.         end
  68.     end
  69. end)
  70.  
  71. for index, checkpoint in ipairs(checkpoints) do
  72.     checkpoint.Touched:Connect(function(hit)
  73.         local player = game.Players:GetPlayerFromCharacter(hit.Parent)
  74.         if not player then return end
  75.         local humanoid = hit.Parent:FindFirstChild("Humanoid")
  76.         if not humanoid or humanoid.Health <= 0 then return end
  77.         local leaderstats = player:FindFirstChild("leaderstats")
  78.         if not leaderstats then return end
  79.         local checkpointStat = leaderstats:FindFirstChild(STAT_NAME)
  80.         if not checkpointStat then return end
  81.  
  82.         if (PREVENT_SKIPPING and checkpointStat.Value + 1 == index) or (not PREVENT_SKIPPING and checkpointStat.Value < index) then
  83.             checkpointStat.Value = index
  84.         end
  85.     end)
  86. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement