Advertisement
oopsrainbow4

Checkpoints

Nov 12th, 2023
821
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.77 KB | None | 0 0
  1. -- First make folder name Checkpoints.
  2. -- Make part with number as stage number in folder
  3.  
  4. -- https://cdn.discordapp.com/attachments/990179256246091812/1173163569412591616/image.png?ex=6562f4b7&is=65507fb7&hm=d5d058b926aa013e1fac9a657c5232b1fb57ac646b4b4a3892910fc07d4891e7&
  5.  
  6. -- Put script in ServerScriptService
  7. -- Public Roblox then go to Game Settings -> Security -> Enabled Studio Access to API Services -> turn On
  8.  
  9. -- https://www.youtube.com/watch?v=xeaaFfgdMPM
  10.  
  11. local checkpoints = workspace:WaitForChild("Checkpoints")
  12.  
  13. local Players = game:GetService("Players")
  14. local RunService = game:GetService("RunService")
  15.  
  16. local DatastoreService = game:GetService("DataStoreService")
  17. local Data = DatastoreService:GetDataStore("1")
  18. local sessionData = {}
  19.  
  20. function PlayerAdded(player)
  21.     local leaderstats = Instance.new("Folder")
  22.     leaderstats.Name
  23.         = "leaderstats"
  24.  
  25.     local stage = Instance.new("NumberValue")
  26.     stage.Name
  27.         = "Stage"
  28.     stage.Parent = leaderstats
  29.  
  30.     local success = nil
  31.     local playerData = nil
  32.     local attempt = 1
  33.  
  34.     repeat
  35.         success, playerData = pcall(function() -- here pcall or protected call is just repeat waiting until the data loads for the player
  36.             return Data:GetAsync(player.UserId)
  37.         end)
  38.  
  39.         attempt += 1
  40.         if not success then
  41.             warn(playerData)
  42.             task.wait(2)
  43.         end
  44.  
  45.     until success or attempt == 5 -- it repeats it until it loads
  46.  
  47.     if success then --if it loads then make the table with their data inside
  48.         print("Data loaded: "..player.Name)
  49.         if not playerData then -- if they have no table then their a new player so we create the table
  50.             print("new player, giving default data")
  51.  
  52.             playerData = {
  53.                 ["Stage"] = 1, --add all your values and stuff inside of the data
  54.  
  55.             }
  56.         end
  57.  
  58.         sessionData[player.UserId] = playerData --set the data to a table with the players id and make to make a variable
  59.     else
  60.         warn("couldnt load data: "..player.Name)
  61.         player:Kick("couldnt load your data, rejoin") --if the data couldnt load we kick them so their not just sitting there forever waiting
  62.     end
  63.  
  64.     stage.Value = sessionData[player.UserId].Stage --here we get the numbervalue created above and get the value of it and set it to the value inside of the table
  65.  
  66.     stage:GetPropertyChangedSignal("Value"):Connect(function()
  67.         sessionData[player.UserId].Stage = stage.Value --update the table value whenever the leaderstat value changes
  68.     end)
  69.  
  70.  
  71.     leaderstats.Parent = player
  72.  
  73. end
  74.  
  75. Players.PlayerAdded:Connect(function(player)
  76.     PlayerAdded(player)
  77.  
  78.     player.CharacterAdded:Connect(function(char)
  79.         local leaderstats = player:WaitForChild("leaderstats")
  80.         local stage = leaderstats.Stage
  81.  
  82.         local hum = char:WaitForChild("Humanoid")
  83.         task.wait()
  84.         char:MoveTo(checkpoints[stage.Value].Position)
  85.  
  86.         hum.Touched:Connect(function(hit)
  87.             if hit.Parent == checkpoints then
  88.                 if tonumber(hit.Name) == stage.Value + 1 then
  89.                     stage.Value += 1
  90.                 end
  91.             end
  92.         end)
  93.     end)
  94. end)
  95.  
  96.  
  97.  
  98. function PlayerLeaving(player)
  99.  
  100.     if sessionData[player.UserId] then
  101.         local success = nil
  102.         local errorMsg = nil
  103.         local attempt = 1
  104.  
  105.  
  106.         repeat
  107.             success, errorMsg = pcall(function()
  108.                 Data:SetAsync(player.UserId, sessionData[player.UserId]) --here is the same as loading data just repeat waits until the data saves
  109.             end)
  110.  
  111.             attempt += 1
  112.             if not success then
  113.                 warn(errorMsg)
  114.                 task.wait(2)
  115.             end
  116.  
  117.         until success or attempt == 5
  118.  
  119.         if success then
  120.             print("Data saved: "..player.Name)
  121.         else
  122.             warn("Cant save: "..player.Name)
  123.         end
  124.  
  125.     end
  126.  
  127. end
  128.  
  129. Players.PlayerRemoving:Connect(PlayerLeaving)
  130.  
  131. function ServerShutdown()
  132.     if RunService:IsStudio() then
  133.         return
  134.     end
  135.  
  136.     for i, player in ipairs(Players:GetPlayers()) do
  137.         task.spawn(function()
  138.             PlayerLeaving(player)
  139.         end)
  140.     end
  141. end
  142.  
  143. game:BindToClose(ServerShutdown)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement