PheonixStudio

Table Datastore saving

Apr 28th, 2020
1,550
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.59 KB | None | 0 0
  1. --[[
  2. Thank you for taking my Datastore Model.
  3. I've organized the Script for you. All you need to do is either Add, Remove or change the Stats and Datastore Name.
  4. There is no Virus nor any Credits to me in this so feel free to do whatever to this Script.
  5. You may also remove the Comments.
  6.  
  7. Thank you again.
  8. Snoopy1333.
  9. ]]
  10.  
  11.  
  12. --// Services
  13. local ds = game:GetService("DataStoreService"):GetDataStore("DataName") -- Replace "DataName" with your Data store name you want.
  14.  
  15. local StatsToAdd = { -- Add or remove stats, You can also set the default start value too.
  16.     ["Cash"] = 100,
  17.     ["Gems"] = 25,
  18.     }
  19.  
  20.  
  21. --// Functions
  22. game.Players.PlayerAdded:Connect(function(plr)
  23.     local Prefix = plr.UserId
  24.    
  25.     local leaderstats = Instance.new("Folder", plr)
  26.     leaderstats.Name = "leaderstats"
  27.    
  28.     for i,v in pairs(StatsToAdd) do -- This will automate the Table for us so we don't need to use Instance alot and changing Values.
  29.         local NewInst = Instance.new("IntValue", leaderstats) -- 2nd Parameter is the Parent of the instance.
  30.         NewInst.Name = i
  31.         NewInst.Value = v
  32.     end
  33.    
  34.     local Data = nil
  35.    
  36.     pcall(function()
  37.         Data = ds:GetAsync(Prefix)
  38.     end)
  39.        
  40.     if Data ~= nil then
  41.         for i,v in pairs(leaderstats:GetChildren()) do
  42.             v.Value = Data[v.Name]
  43.         end
  44.     end
  45. end)
  46.  
  47. game.Players.PlayerRemoving:Connect(function(plr)
  48.     local Prefix = plr.UserId
  49.    
  50.     local leaderstats = plr.leaderstats
  51.    
  52.     pcall(function()
  53.         if leaderstats then
  54.             local SaveData = {}
  55.            
  56.             for i,v in pairs(leaderstats:GetChildren()) do
  57.                 SaveData[v.Name] = v.Value
  58.             end
  59.            
  60.             ds:SetAsync(Prefix, SaveData)
  61.         end
  62.     end)
  63. end)
Advertisement
Add Comment
Please, Sign In to add comment