Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local module = {}
- module.__index = module
- local dataTable = {}
- dataTable.Players = {}
- dataTable.Mobs = {}
- local starterPlayerData = function(userId) print('OLA9233') local playerData = {Data = {
- Inventory={};
- Hotbar={};
- Gold=0;
- Class='Undefined';
- Stats={Max=1;Agility=0;Defense=0;Strength=0;};
- };
- UserId = userId
- } return playerData end
- local Players = game:GetService('Players')
- local DataStoreService = game:GetService('DataStoreService')
- local TextCompressor = require(script.Parent:WaitForChild('TextCompressor'))
- local newInstance = Instance.new
- local stringSub = string.sub
- local HttpService = game:GetService('HttpService')
- local DataStore = DataStoreService:GetDataStore('[4]-TEST-DataStore')
- local function Encode(tbl)
- if not tbl then return end
- if typeof(tbl) == 'table' then
- return HttpService:JSONEncode(tbl)
- end
- end
- local function Decode(tbl)
- if not tbl then return end
- if typeof(tbl) == 'string' then
- return HttpService:JSONDecode(tbl)
- end
- end
- local function FindFirstChild(obj,childName)
- if childName and obj then
- return obj:FindFirstChild(childName)
- end
- end
- local function reviewDataStructure(data,indexName)
- local dataStructure = indexName and starterPlayerData()[indexName] or starterPlayerData()
- for index,value in pairs(dataStructure) do
- if data[index] == nil then
- data[index] = value
- end
- end
- return
- end
- local function openTable(tbl)
- local finalTable = {}
- for index,value in pairs(tbl) do
- print(index,value)
- finalTable[#finalTable] = {index,value}
- if typeof(dataTable[index]) == 'table' then
- local newTable = openTable(dataTable[index])
- for i = 1,#newTable do
- finalTable[#finalTable] = newTable[i]
- end
- end
- end
- return finalTable
- end
- local function tableToObject(plrData,tbl,folder)
- for index,value in pairs(tbl) do
- if typeof(value) == 'table' then
- local instanceFolder = newInstance('Folder')
- instanceFolder.Name = index
- instanceFolder.Parent = folder
- tableToObject(plrData,value,instanceFolder)
- else
- local typeOf = typeof(value)
- local FirstLetter = (stringSub(typeOf,1,1)):upper()
- local WithoutFirstLetter = stringSub(typeOf,2)
- if WithoutFirstLetter == 'oolean' then WithoutFirstLetter = 'ool' end
- local ClassName = FirstLetter..WithoutFirstLetter..'Value'
- local valueObject = newInstance(ClassName)
- valueObject.Value = value
- valueObject.Name = index
- valueObject.Changed:Connect(function(newValue)
- tbl[index] = newValue
- end)
- valueObject.Parent = folder
- end
- end
- end
- local function objectToTable(folder)
- local dataTable = {}
- for index,value in pairs(folder:GetChildren()) do
- if value:IsA('Folder') then
- dataTable[value.Name] = objectToTable(value)
- else
- print(value.Name,value.Value)
- dataTable[value.Name] = value.Value
- end
- end
- return dataTable
- end
- function module.loadData(object)
- local data
- if object:IsA('Player') and not dataTable['Players'][object.UserId] then
- data = Decode(TextCompressor:Decompress(DataStore:GetAsync(object.UserId))) or starterPlayerData(object.UserId)
- local reviewingDataStructure = reviewDataStructure(data)
- dataTable['Players'][object.UserId] = data
- print(Encode(data))
- elseif object:IsA('Player') and dataTable['Players'][object.UserId] then
- return dataTable['Players'][object.UserId]
- end
- return setmetatable(data,module)
- end
- function module.unloadData(object)
- if object:IsA('Player') and dataTable['Players'][object.UserId] then
- dataTable['Players'][object.UserId] = nil
- elseif not object:IsA('Player') and dataTable['Mobs'][object] then
- dataTable['Mobs'][object] = nil
- end
- end
- function module:createDataFolder(folderParent,folderName)
- self.DataObject = folderName
- print(self.DataObject)
- local tableObjects = self.Data
- local instanceFolder = newInstance('Folder')
- instanceFolder.Name = folderName
- tableToObject(self,tableObjects,instanceFolder)
- instanceFolder.Parent = folderParent
- end
- function module:saveData()
- local plr = Players:GetPlayerByUserId(self.UserId)
- local dataToSave = TextCompressor:Compress(Encode(self.Data))
- print(Encode(self.Data),dataToSave)
- DataStore:UpdateAsync(self.UserId,function(old)
- print(1)
- if old then
- print(2)
- local oldDecoded = Decode(TextCompressor:Decompress(old))
- print(3)
- if dataToSave.Data.Max < oldDecoded.Data.Max then
- return oldDecoded
- end
- end
- print('JULIA')
- return dataToSave
- end)
- print('OIOI')
- end
- function module:Unload()
- print('AAAAAAA')
- if self.UserId then
- print('HUH')
- dataTable['Players'][self.UserId] = nil
- end
- end
- return module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement