Advertisement
Joriangames

Multiple values datastore

May 11th, 2021
2,349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.74 KB | None | 0 0
  1. --[[
  2. Thanks for using this script
  3. This script is made by Joriangames/Problox Studio Scripts
  4. Want to know how to use this and script explanation?
  5. Watch my tutorial: https://youtu.be/ftCgYgQ-qpM
  6.  
  7. Don't forget to turn on API services and Publish the game!
  8. ]]
  9.  
  10. local DSS = game:GetService("DataStoreService")
  11. local statsDS = DSS:GetDataStore("statsDS")
  12.  
  13. game.Players.PlayerAdded:Connect(function(plr)
  14.     local lead = Instance.new("Folder")
  15.     lead.Name = "leaderstats"
  16.     lead.Parent = plr
  17.    
  18.     local cash = Instance.new("IntValue")
  19.     cash.Name = "Cash"
  20.     cash.Parent = lead
  21.     cash.Value = 0
  22.    
  23.     local diam = Instance.new("IntValue")
  24.     diam.Name = "Diamonds"
  25.     diam.Parent = lead
  26.     cash.Value = 0
  27.    
  28.     local Reb = Instance.new("IntValue")
  29.     Reb.Name = "Rebirths"
  30.     Reb.Parent = lead
  31.     cash.Value = 0
  32.    
  33.     local data
  34.    
  35.     local succ, err = pcall(function()
  36.         data = statsDS:GetAsync(plr.UserId.."-stats")
  37.     end)
  38.    
  39.     if succ then
  40.         print("success")
  41.         if data then                    --if there was some data then
  42.             print("Got the data from the player datastore")
  43.             cash.Value = data[1]
  44.             diam.Value = data[2]
  45.             Reb.Value = data[3]        
  46.         end
  47.     else
  48.         print("There was an error while checking: "..plr.Name)
  49.         warn(err)
  50.     end
  51.    
  52. end)
  53.  
  54.  
  55. game.Players.PlayerRemoving:Connect(function(plr)
  56.     print("Player is leaving the game!")
  57.     local cash = plr.leaderstats.Cash.Value
  58.     local diam = plr.leaderstats.Diamonds.Value
  59.     local reb = plr.leaderstats.Rebirths.Value
  60.     print("Got all the values")
  61.    
  62.    
  63.     local data = {cash, diam, reb}
  64.    
  65.     local succ, err = pcall(function()
  66.         statsDS:SetAsync(plr.UserId.."-stats", data)
  67.     end)
  68.    
  69.     if succ then
  70.         print("Succesfully saved the data!")
  71.     else
  72.         print("There was an error while saving player leaderstats!")
  73.         warn(err)
  74.     end
  75. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement