Hello, I made a custom datastore module with three functions:
-
Initialize data
-
Saving data
-
Updating Session data with optional UpdateAsync
-
Saving all player data at once
-
Might add an extra layer of backup using SessionData but I don’t think it’s necessary.
I tried to make this as reliable as possible with retries when neccessary, please correct me if anything is wrong with my code
local dataFunctions = {}
dataFunctions.SessionData = {}
SessionData = dataFunctions.SessionData
function dataFunctions.InitializeData(player, datastore, datatable)
local DataStoreService = game:GetService("DataStoreService")
local UserId = "Player_"..player.UserId
SessionData.UserId = {}
local DataStore = DataStoreService:GetDataStore(datastore)
local data
local success, err = pcall(function()
data = DataStore:GetAsync(UserId)
end)
if success then
if data and datatable and player then
SessionData.UserId = data
for i, v in ipairs(datatable) do
v = data[i]
end
print("Loaded stats.")
end
else
local tries = 0
local success
repeat
tries = tries + 1
success = pcall(function()
data = DataStore:GetAsync(UserId)
end)
if not success then
wait(1)
end
until tries > 3 or success
if success then
if data and player and datatable then
SessionData.UserId = data
for i, v in ipairs(datatable) do
v = data[i]
end
print("Retry successful.")
end
else
warn("Failed to initialize data.")
end
end
end
function dataFunctions.SaveData(player, datastore, datatable)
local DataStoreService = game:GetService("DataStoreService")
local UserId = "Player_"..player.UserId
local DataStore = DataStoreService:GetDataStore(datastore)
local data
if datatable then
data = datatable
SessionData.UserId = datatable
else
data = SessionData.UserId
end
local success, err = pcall(function()
DataStore:SetAsync(UserId, data)
end)
if success then
print("Successfully saved data.")
else
local tries = 0
local success
repeat
tries = tries + 1
success = pcall(function()
DataStore:SetAsync(UserId, data)
end)
if not success then
wait(1)
end
until tries > 3 or success
if success then
print("Retry successful.")
else
warn("Failed to save data.")
end
end
end
function dataFunctions.UpdateSession(player, datastore, datatable, UpdateAsync)
local DataStoreService = game:GetService("DataStoreService")
local UserId = "Player_"..player.UserId
SessionData.UserId = datatable
if UpdateAsync then
DataStore = DataStoreService:GetDataStore(datastore)
local success, err = pcall(function()
DataStore:UpdateAsync(UserId, function()
new = datatable
return new
end)
end)
if success then
print("Updated data.")
else
local tries = 0
local success
repeat
tries = tries + 1
success = pcall(function()
DataStore:UpdateAsync(UserId, function(old)
new = datatable
return new
end)
end)
if not success then
wait(1)
end
until tries > 3 or success
if success then
print("Retry successful.")
else
warn("Failed to update data.")
end
end
end
end
function dataFunctions.SaveAllData(datastore, datatable)
local DataStoreService = game:GetService(“DataStoreService”)
local DataStore = DataStoreService:GetDataStore(datastore)
for _, player in pairs(game.Players:GetChildren()) do
if player and datatable then
local UserId = “Player_”..player.UserId
SessionData.UserId = datatable
local success, err = pcall(function()
DataStore:SetAsync(UserId, datatable)
end)
if success then
print("Saved data for " .. player.Name .. ".")
else
local tries = 0
local success
repeat
tries = tries + 1
success = pcall(function()
DataStore:SetAsync(UserId, datatable)
end)
if not success then
wait(1)
end
until tries > 3 or success
if success then
print("Retry successful.")
else
warn("Failed to save data for " .. player.Name .. ".")
end
end
end
end
end
return dataFunctions