DanielSiqueira

datastore

Jan 27th, 2020
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.62 KB | None | 0 0
  1. local module = {}
  2. module.__index = module
  3.  
  4. local dataTable = {}
  5. dataTable.Players = {}
  6. dataTable.Mobs = {}
  7. local starterPlayerData = function(userId) print('OLA9233') local playerData = {Data = {
  8.  
  9.     Inventory={};
  10.     Hotbar={};
  11.     Gold=0;
  12.     Class='Undefined';
  13.     Stats={Max=1;Agility=0;Defense=0;Strength=0;};
  14.  
  15. };
  16.  
  17.     UserId = userId
  18.  
  19. } return playerData end
  20.  
  21. local Players = game:GetService('Players')
  22. local DataStoreService = game:GetService('DataStoreService')
  23. local TextCompressor = require(script.Parent:WaitForChild('TextCompressor'))
  24. local newInstance = Instance.new
  25. local stringSub = string.sub
  26. local HttpService = game:GetService('HttpService')
  27.  
  28. local DataStore = DataStoreService:GetDataStore('[4]-TEST-DataStore')
  29.  
  30. local function Encode(tbl)
  31.     if not tbl then return end
  32.     if typeof(tbl) == 'table' then
  33.         return HttpService:JSONEncode(tbl)
  34.     end
  35. end
  36.  
  37. local function Decode(tbl)
  38.     if not tbl then return end
  39.     if typeof(tbl) == 'string' then
  40.         return HttpService:JSONDecode(tbl)
  41.     end
  42. end
  43.  
  44. local function FindFirstChild(obj,childName)
  45.     if childName and obj then
  46.         return obj:FindFirstChild(childName)
  47.     end
  48. end
  49.  
  50. local function reviewDataStructure(data,indexName)
  51.     local dataStructure = indexName and starterPlayerData()[indexName] or starterPlayerData()
  52.     for index,value in pairs(dataStructure) do
  53.         if data[index] == nil then
  54.             data[index] = value
  55.         end
  56.     end
  57.     return
  58. end
  59.  
  60. local function openTable(tbl)
  61.     local finalTable = {}
  62.     for index,value in pairs(tbl) do
  63.         print(index,value)
  64.         finalTable[#finalTable] = {index,value}
  65.         if typeof(dataTable[index]) == 'table' then
  66.             local newTable = openTable(dataTable[index])
  67.             for i = 1,#newTable do
  68.                 finalTable[#finalTable] = newTable[i]
  69.             end
  70.         end
  71.     end
  72.     return finalTable
  73. end
  74.  
  75. local function tableToObject(plrData,tbl,folder)
  76.     for index,value in pairs(tbl) do
  77.         if typeof(value) == 'table' then
  78.             local instanceFolder = newInstance('Folder')
  79.             instanceFolder.Name = index
  80.             instanceFolder.Parent = folder
  81.             tableToObject(plrData,value,instanceFolder)
  82.         else
  83.             local typeOf = typeof(value)
  84.             local FirstLetter = (stringSub(typeOf,1,1)):upper()
  85.             local WithoutFirstLetter = stringSub(typeOf,2)
  86.            
  87.             if WithoutFirstLetter == 'oolean' then WithoutFirstLetter = 'ool' end
  88.             local ClassName = FirstLetter..WithoutFirstLetter..'Value'
  89.            
  90.             local valueObject = newInstance(ClassName)
  91.             valueObject.Value = value
  92.             valueObject.Name = index
  93.             valueObject.Changed:Connect(function(newValue)
  94.                 tbl[index] = newValue
  95.             end)
  96.             valueObject.Parent = folder
  97.         end
  98.        
  99.     end
  100. end
  101.  
  102. local function objectToTable(folder)
  103.     local dataTable = {}
  104.     for index,value in pairs(folder:GetChildren()) do
  105.         if value:IsA('Folder') then
  106.             dataTable[value.Name] = objectToTable(value)
  107.         else
  108.             print(value.Name,value.Value)
  109.             dataTable[value.Name] = value.Value
  110.         end
  111.     end
  112.     return dataTable
  113. end
  114.  
  115. function module.loadData(object)
  116.     local data
  117.     if object:IsA('Player') and not dataTable['Players'][object.UserId] then
  118.         data = Decode(TextCompressor:Decompress(DataStore:GetAsync(object.UserId))) or starterPlayerData(object.UserId)
  119.         local reviewingDataStructure = reviewDataStructure(data)
  120.         dataTable['Players'][object.UserId] = data
  121.         print(Encode(data))
  122.     elseif object:IsA('Player') and dataTable['Players'][object.UserId] then
  123.         return dataTable['Players'][object.UserId]
  124.     end
  125.     return setmetatable(data,module)
  126. end
  127.  
  128. function module.unloadData(object)
  129.     if object:IsA('Player') and dataTable['Players'][object.UserId] then
  130.         dataTable['Players'][object.UserId] = nil
  131.     elseif not object:IsA('Player') and dataTable['Mobs'][object] then
  132.         dataTable['Mobs'][object] = nil
  133.     end
  134. end
  135.  
  136. function module:createDataFolder(folderParent,folderName)
  137.     self.DataObject = folderName
  138.     print(self.DataObject)
  139.     local tableObjects = self.Data
  140.     local instanceFolder = newInstance('Folder')
  141.     instanceFolder.Name = folderName
  142.    
  143.     tableToObject(self,tableObjects,instanceFolder)
  144.     instanceFolder.Parent = folderParent
  145. end
  146.  
  147. function module:saveData()
  148.     local plr = Players:GetPlayerByUserId(self.UserId)
  149.     local dataToSave = TextCompressor:Compress(Encode(self.Data))
  150.     print(Encode(self.Data),dataToSave)
  151.     DataStore:UpdateAsync(self.UserId,function(old)
  152.         print(1)
  153.         if old then
  154.             print(2)
  155.             local oldDecoded = Decode(TextCompressor:Decompress(old))
  156.             print(3)
  157.             if dataToSave.Data.Max < oldDecoded.Data.Max then
  158.                 return oldDecoded
  159.             end
  160.         end
  161.         print('JULIA')
  162.         return dataToSave
  163.     end)
  164.     print('OIOI')
  165. end
  166.  
  167. function module:Unload()
  168.     print('AAAAAAA')
  169.     if self.UserId then
  170.         print('HUH')
  171.         dataTable['Players'][self.UserId] = nil
  172.     end
  173. end
  174.  
  175. return module
Add Comment
Please, Sign In to add comment