HowToRoblox

ObbyHandler

Jan 30th, 2021 (edited)
2,502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.56 KB | None | 0 0
  1. game.Players.CharacterAutoLoads = false
  2.  
  3.  
  4. local dss = game:GetService("DataStoreService")
  5. local obbyDS = dss:GetDataStore("ObbyData")
  6.  
  7.  
  8. local checkpoints = workspace:WaitForChild("Checkpoints")
  9.  
  10.  
  11.  
  12. game.Players.PlayerAdded:Connect(function(plr)
  13.    
  14.     local stageData
  15.     pcall(function()
  16.         stageData = obbyDS:GetAsync("Stage-" .. plr.UserId)
  17.     end)
  18.    
  19.    
  20.     local ls = Instance.new("Folder")
  21.     ls.Name = "leaderstats"
  22.     ls.Parent = plr
  23.    
  24.     local stage = Instance.new("StringValue")
  25.     stage.Name = "Stage"
  26.     stage.Value = stageData or "1"
  27.     stage.Parent = ls
  28.    
  29.    
  30.     plr.CharacterAdded:Connect(function(char)
  31.        
  32.         local hrp = char:WaitForChild("HumanoidRootPart")
  33.        
  34.         hrp:GetPropertyChangedSignal("CFrame"):Wait()
  35.        
  36.         hrp.CFrame = checkpoints[stage.Value].CFrame
  37.        
  38.        
  39.         char.Humanoid.Died:Connect(function()
  40.            
  41.             plr:LoadCharacter()
  42.         end)
  43.        
  44.        
  45.         char.Humanoid.Touched:Connect(function(part)
  46.            
  47.             if part.Parent == checkpoints then
  48.                
  49.                
  50.                 if part.Name ~= "End" and stage.Value ~= "End" then
  51.                    
  52.                     if tonumber(part.Name) > tonumber(stage.Value) then
  53.                        
  54.                         stage.Value = part.Name
  55.                     end
  56.                    
  57.                 elseif part.Name == "End" then
  58.                     stage.Value = part.Name
  59.                 end
  60.             end
  61.         end)
  62.     end)
  63.    
  64.    
  65.     plr:LoadCharacter()
  66. end)
  67.  
  68.  
  69. local function saveData(plr)
  70.    
  71.     pcall(function()
  72.         obbyDS:SetAsync("Stage-" .. plr.UserId, plr.leaderstats.Stage.Value)
  73.     end)
  74. end
  75.  
  76.  
  77. game.Players.PlayerRemoving:Connect(saveData)
  78.  
  79. game:BindToClose(function()
  80.    
  81.    
  82.     for i, plr in pairs(game.Players:GetPlayers()) do
  83.        
  84.         saveData(plr)
  85.     end
  86. end)
  87.  
Add Comment
Please, Sign In to add comment