Tweak16

ROBLOX Scripting | SAVING OBBY CHECKPOINTS DATA

Dec 31st, 2020 (edited)
4,228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.95 KB | None | 0 0
  1. -- Made by Tweakified, Neonblox Games
  2. -- Video: https://www.youtube.com/watch?v=k_SGagxVOmc
  3.  
  4. local Players = game:GetService("Players")
  5. local DataStoreService = game:GetService("DataStoreService")
  6. local ObbyDataStore = DataStoreService:GetDataStore("ObbyDataStore")
  7.  
  8. local Checkpoints = workspace:WaitForChild("Checkpoints")
  9. local inGameStartupPlayers = {}
  10. local CurrentStage = {}
  11. local TouchDb = {}
  12.  
  13. local function NewCharacter(player, char)
  14.     local TempCurrentStage = CurrentStage[player.UserId]
  15.     if TempCurrentStage ~= nil then
  16.         local TempCheckpoint = Checkpoints:FindFirstChild(TempCurrentStage)
  17.         if TempCheckpoint ~= nil then
  18.             repeat wait(0.1) until char.PrimaryPart ~= nil
  19.             char:SetPrimaryPartCFrame(CFrame.new(TempCheckpoint.Position + Vector3.new(0, 3, 0)) * CFrame.Angles(0, math.rad(TempCheckpoint.Orientation.Y) + math.rad(90), 0))
  20.         end
  21.     end
  22. end
  23.  
  24. local function NewPlayer(player)
  25.     local success, stage = pcall(function()
  26.         return (ObbyDataStore:GetAsync(player.UserId)) or 1
  27.     end)
  28.  
  29.     CurrentStage[player.UserId] = stage
  30.  
  31.     local leaderstats = Instance.new("Folder", player)
  32.     leaderstats.Name = "leaderstats"
  33.     local Stage = Instance.new("IntValue", leaderstats)
  34.     Stage.Name = "Stage"
  35.     Stage.Value = stage
  36.  
  37.     local TempChar = player.Character
  38.     if TempChar ~= nil then
  39.         NewCharacter(player, TempChar)
  40.     end
  41.     player.CharacterAdded:Connect(function(char)
  42.         NewCharacter(player, char)
  43.     end)
  44. end
  45.  
  46. Players.PlayerAdded:Connect(function(player)
  47.     if inGameStartupPlayers[player] == nil then
  48.         NewPlayer(player)
  49.     end
  50. end)
  51.  
  52. Players.PlayerRemoving:Connect(function(player)
  53.     local success = pcall(function()
  54.         ObbyDataStore:SetAsync(player.UserId, CurrentStage[player.UserId])
  55.     end)
  56.     CurrentStage[player.UserId] = nil
  57. end)
  58.  
  59. for i,v in pairs(Checkpoints:GetChildren()) do
  60.     local StageNum = tonumber(v.Name)
  61.     v.Touched:Connect(function(hit)
  62.         local char = hit.Parent
  63.         if char ~= nil then
  64.             local Humanoid = char:FindFirstChildOfClass("Humanoid")
  65.             if Humanoid ~= nil and Humanoid.Health > 0 then
  66.                 local player = Players:GetPlayerFromCharacter(char)
  67.                 if player ~= nil and (TouchDb[player.UserId] or 0) + 1 <= os.time() then
  68.                     TouchDb[player.UserId] = os.time()
  69.                     local TempCurrentStage = CurrentStage[player.UserId]
  70.                     if TempCurrentStage == StageNum - 1 then
  71.                         CurrentStage[player.UserId] = StageNum
  72.                         local TempLeaderstats = player:FindFirstChild("leaderstats")
  73.                         if TempLeaderstats ~= nil then
  74.                             local TempStage = TempLeaderstats:FindFirstChild("Stage")
  75.                             if TempStage ~= nil then
  76.                                 TempStage.Value = StageNum
  77.                             end
  78.                         end
  79.                     end
  80.                 end
  81.             end
  82.         end
  83.     end)
  84. end
  85.  
  86. inGameStartupPlayers = Players:GetPlayers()
  87. for i,v in pairs(inGameStartupPlayers) do
  88.     spawn(function()
  89.         NewPlayer(v)
  90.     end)
  91. end
  92.  
  93. game:BindToClose(function()
  94.     for i,v in pairs(Players:GetPlayers()) do
  95.         ObbyDataStore:SetAsync(v.UserId, CurrentStage[v.UserId])
  96.     end
  97.     wait(1)
  98. end)
  99.  
  100. inGameStartupPlayers = {}
Add Comment
Please, Sign In to add comment