Advertisement
Guest User

Untitled

a guest
Dec 17th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.28 KB | None | 0 0
  1. local dss = game:GetService('DataStoreService')
  2. local http = game:GetService('HttpService')
  3.  
  4. local data = {}
  5. local baseData = {
  6.     ["Coins"] = 15,
  7.     ["Gems"] = 2,
  8.     ["Level"] = 0,
  9.     ["Hats"] = {},
  10.     ["Masks"] = {},
  11.     ["Guns"] = {},
  12.     ["Ball"] = {}
  13. }
  14. function save(player, dataStoreName, tableToSave)
  15.     local ds = dss:GetDataStore(dataStoreName)
  16.     local key = "Player - "..player.UserId
  17.     local encodeStats = http:JSONEncode(tableToSave)
  18.     ds:SetAsync(key, encodeStats)
  19. end
  20. function loadPlayer(player, dataStoreName)
  21.     local ds = dss:GetDataStore(dataStoreName)
  22.     local key = "Player - "..player.UserId
  23.     local encodedValues = ds:GetAsync(key)
  24.     local savedValues = nil
  25.     if encodedValues then
  26.         savedValues = http:JSONDecode(encodedValues)
  27.     end
  28.     if savedValues then
  29.         for i,c in pairs(savedValues) do
  30.             if type(c) == "table" then
  31.                 for _, p in pairs(c) do
  32.                     print(p)
  33.                 end
  34.             else
  35.                 print(c)
  36.             end
  37.         end
  38.         return savedValues
  39.     else
  40.         save(player, dataStoreName, baseData)
  41.     end
  42. end
  43. game.Players.PlayerAdded:connect(function(plr)
  44.     local loadedTable = loadPlayer(plr, "Test")
  45.     data[plr.UserId] = loadedTable
  46.     wait()
  47.     local d = data[plr.UserId]
  48.     print(d)
  49.     print(d["Coins"])
  50. end)
  51. game.Players.PlayerRemoving:connect(function(plr)
  52.     save(plr, "Test", data[plr.UserId])
  53. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement