Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local module = {}
- module.__index = module
- module.EphemeralDataStore = script:WaitForChild('EphemeralDataStore')
- local Events = script:WaitForChild('Events')
- local dataTable = {}
- dataTable.Players = {}
- dataTable.Mobs = {}
- local starterPlayerData = function(userId) print('Creating new data...') local playerData = {Data = {
- Level=0;
- Inventory={};
- Runes={['Precision']=false;['Final decision']=false;['Teamwork']=true;['Assassin Instinct']=false;['Shield of Hope']=true;['Heal of God']=false;};
- ['Trex Points']=0;
- ['Rank']='Unranked';
- };
- UserId = userId;
- Clan = {ID=0;Name='<>'};
- } return playerData end
- local Players = game:GetService('Players')
- local DataStoreService = game:GetService('DataStoreService')
- local newInstance = Instance.new
- local stringSub = string.sub
- local HttpService = game:GetService('HttpService')
- local DataStore = DataStoreService:GetDataStore('[3]-TEST-DataStore')
- local DataStructure = starterPlayerData()
- 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 DataStructure[indexName] or not indexName and DataStructure or nil
- if not dataStructure then return end
- for index,value in pairs(dataStructure) do
- if data[index] == nil then
- data[index] = value
- elseif typeof(data[index]) == 'table' then
- reviewDataStructure(data[index],index)
- end
- end
- return
- end
- local function openTable(tbl)
- local finalTable = {}
- for index,value in pairs(tbl) do
- 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
- local a = DataStore:GetAsync(object.UserId) or ''
- data = Decode(DataStore:GetAsync(object.UserId)) or starterPlayerData(object.UserId)
- local reviewingDataStructure = reviewDataStructure(data)
- dataTable['Players'][object.UserId] = data
- local onLoadEvent = Events.onDataLoad:Fire(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,shouldSave)
- if object:IsA('Player') and dataTable['Players'][object.UserId] then
- if shouldSave then local waitForFunction = dataTable['Players'][object.UserId]:saveData() end
- dataTable['Players'][object.UserId] = nil
- elseif not object:IsA('Player') and dataTable['Mobs'][object] then
- dataTable['Mobs'][object] = nil
- end
- return true
- end
- function module:createDataFolder(folderParent,folderName)
- self.DataObject = folderName
- local tableObjects = self.Data
- local instanceFolder = newInstance('Folder')
- instanceFolder.Name = folderName
- tableToObject(self,tableObjects,instanceFolder)
- instanceFolder.Parent = folderParent
- end
- function module:saveData(shouldUnload)
- if self.isSaving then return end
- local plr = Players:GetPlayerByUserId(self.UserId)
- self.isSaving = true
- local dataToSave = {}
- dataToSave.UserId = self.UserId
- dataToSave.Data = self.Data
- dataToSave.Clan = self.Clan
- dataToSave = Encode(dataToSave)
- print('Attempting to save data',dataToSave)
- local waitForUpdate = DataStore:UpdateAsync(self.UserId,function(old)
- print('Starting save process')
- if old then
- local oldDecoded = Decode(old)
- if self.Data.Level < oldDecoded.Data.Level then
- print('Backup to old data...')
- return old
- end
- end
- print('Finalizing save process')
- return dataToSave
- end)
- self.isSaving = nil
- if shouldUnload then self:Unload() end
- return true
- end
- function module:Unload(shouldSave)
- if self.UserId then
- if shouldSave then self:saveData() end
- dataTable['Players'][self.UserId] = nil
- return true
- end
- end
- return module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement