Hi,
I recently got my datastore system upgraded to be more sophisticated. To me it personally seems that it is not possible for dataloss to occur, however it still very rarely does (one player has everything reset) however most of the time players keep their data.
Below is the ModuleScript that handles the general loading and saving of data, can anyone think why even after this datastore optimisation the issue still occurs?
Ive also not had much luck in finding a similar issue on the devforum (seemingly) which im then slightly confused as to how its happening, its quite a big problem for the game
Thanks
Code below:
--//This module is responsible for saving and loading data
local module = {}
local DataStoreService = game:GetService('DataStoreService')
local JSONExpert = require(script.JSONExpert)
function module:Load(datastore_key, i, initializer, tries)
i = (typeof(i) == 'Instance' and i:IsA('Player') and tostring(i.UserId)) or i
initializer = initializer or {}
tries = tries or math.huge
local datastore = DataStoreService:GetDataStore(datastore_key)
local data do
for _ = 1,tries do
local success, error = pcall(function()
data = datastore:GetAsync(i)
end)
if (success == true) then
if (data == nil) then
data = initializer
else
data = JSONExpert:JSONDecode(data)
if (next(data) == nil) then
data = initializer
end
end
break
else
warn(error)
end
end
end
return data
end
function module:Save(datastore_key, i, v, tries)
i = (typeof(i) == 'Instance' and i:IsA('Player') and tostring(i.UserId)) or i
v = v or {}
tries = tries or math.huge
local datastore = DataStoreService:GetDataStore(datastore_key)
local success, error
v = JSONExpert:JSONEncode(v)
for _ = 1,tries do
success, error = pcall(function()
datastore:SetAsync(i, v)
end)
if (success == true) then
return true;
else
warn(error);
end
end
return false;
end
--//Encodes a table of values into a heiarchy of value objects
function module:EncodeValues(Stats, parent)
for i,v in pairs(Stats) do
if (typeof(v) == 'table') then
local folder = Instance.new('Folder')
folder.Name = i
folder.Parent = parent
self:EncodeValues(v, folder)
else
local type_i = typeof(v) do
if (type_i == 'boolean') then
type_i = 'bool'
end
type_i = type_i:sub(1,1):upper()..type_i:sub(2) --//Capitalizes first letter
end
local value = Instance.new(string.format('%sValue', type_i));
value.Name = i
value.Value = v
value.Parent = parent
end
end
end
--//Decodes a heiarchy of value objects into a table of values
function module:DecodeValues(parent, table)
table = table or {}
for _,obj in ipairs(parent:GetChildren()) do
if (obj:IsA('Folder')) then
table[obj.Name] = self:DecodeValues(obj, {})
elseif (obj:IsA('ValueBase')) then
table[obj.Name] = obj.Value
end
end
return table
end
return module