Joriangames

ObbyHandler script with datastore

Dec 10th, 2022
1,576
-1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.89 KB | None | 0 1
  1. --[[
  2. Thanks for using this script
  3. This script was made by Joriangames/BloxianCode
  4. Want to know how to use this and script explanation?
  5. Check the video here: https://youtu.be/SIW3Vq-DjZI
  6. ]]
  7.  
  8. local DS = game:GetService("DataStoreService")
  9. local stageDS = DS:GetDataStore("stageDS")
  10.  
  11. local spawnsFolder = game.Workspace.Spawns
  12.  
  13. game.Players.PlayerAdded:Connect(function(plr)
  14.     local lead = Instance.new("Folder", plr)
  15.     lead.Name = 'leaderstats'
  16.    
  17.     local stage = Instance.new("IntValue", lead)
  18.     stage.Name = "Stage"
  19.     stage.Value = 0
  20.    
  21.     plr.CharacterAdded:Connect(function(char)
  22.         local hum = char:WaitForChild("Humanoid")
  23.         wait()
  24.         char:MoveTo(spawnsFolder[stage.Value].Position)
  25.        
  26.         hum.Touched:Connect(function(hit)
  27.             if hit.Parent == spawnsFolder then
  28.                 print("Player touched a spawn")
  29.                 if tonumber(hit.Name) == stage.Value +1 then
  30.                     stage.Value = stage.Value +1
  31.                 end
  32.             end
  33.         end)
  34.     end)
  35.    
  36.     local data
  37.    
  38.     local success, err = pcall(function()
  39.         data = stageDS:GetAsync(plr.UserId.."-stage")
  40.     end)
  41.    
  42.     if success then
  43.         if data then
  44.             print("There was a value saved")
  45.             stage.Value = data
  46.            
  47.             local char = plr.Character or plr.CharacterAdded:Wait()
  48.             char:WaitForChild("HumanoidRootPart").CFrame = spawnsFolder[data].CFrame + Vector3.new(0,5,0)
  49.         else
  50.             local char = plr.Character
  51.             char:WaitForChild("HumanoidRootPart").CFrame = spawnsFolder["0"].CFrame + Vector3.new(0,5,0)
  52.         end
  53.     else
  54.         warn("An error occurred while checking datastore for "..plr.Name)
  55.     end
  56. end)
  57.  
  58. game.Players.PlayerRemoving:Connect(function(plr)
  59.     print(plr.Name.." is leaving the game")
  60.     local data = plr.leaderstats.Stage.Value
  61.    
  62.     local succ, err = pcall(function()
  63.         stageDS:SetAsync(plr.UserId.."-stage", data)
  64.     end)
  65.    
  66.     if succ then
  67.         print("Succesfully saved the stage to the datastore")
  68.     else
  69.         print("An error occured while saving the stage-data for "..plr.Name)
  70.     end
  71. end)
Advertisement
Add Comment
Please, Sign In to add comment