Figured I should post my new DataStore API. This prevents the current data loss issue (nil cache), but it does require you to initialize the values first to make them non-nil.
Variant result safeGetAsync(GlobalDataStore DataStore, string key, bool allowNil)
Variant value safeSetAsync(GlobalDataStore DataStore, string key, Variant value)
Variant value safeUpdateAsync(GlobalDataStore DataStore, string key, Variant value, Function transformFunction, bool allowNil)
Variant transformedValue transformFunction(Variant originalValue, Variant value)
function safeGetAsync(DataStore, key, allowNil, fails)
local success, result = pcall(function()
return DataStore:GetAsync(key)
end)
if not success or result == nil and not allowNil then
warn(tostring(result) .. " Key =" .. key)
local errorCode = tonumber(string.sub(tostring(result), 1, 3)) or 0
if math.floor(errorCode/100) == 1 then
return
else
wait((fails or 1)*6)
return safeGetAsync(DataStore, key, allowNil, (fails or 1) + 1)
end
else
return result
end
end
function safeSetAsync(DataStore, key, value, fails)
local success, result = pcall(function()
return DataStore:SetAsync(key, value)
end)
if not success then
warn(tostring(result) .. " Key =" .. key)
local errorCode = tonumber(string.sub(tostring(result), 1, 3)) or 0
if math.floor(errorCode/100) == 1 then
return
else
wait((fails or 1)*6)
return safeSetAsync(DataStore, key, value, (fails or 1) + 1)
end
else
return value
end
end
function safeUpdateAsync(DataStore, key, value, transformFunction, allowNil, fails)
local success, result = pcall(function()
return DataStore:UpdateAsync(key, function(originalValue)
if originalValue == nil and not allowNil then
return
else
return transformFunction(originalValue, value)
end
end)
end)
if not success or result == nil and not allowNil then
warn(tostring(result) .. " Key =" .. key)
local errorCode = tonumber(string.sub(tostring(result), 1, 3)) or 0
if math.floor(errorCode/100) == 1 then
return
else
wait((fails or 1)*6)
return safeUpdateAsync(DataStore, key, value, transformFunction, allowNil, (fails or 1) + 1)
end
else
return value
end
end