Advertisement
DanielSiqueira

Daniel Siqueira - DataStoreSystem

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