So, I am kind of a beginner I guess and I am writing a data saving module I could use for future projects. I want to ask more experienced scripters to review this code and let me know how can I improve it.
Script:
local DataStoreService = game:GetService("DataStoreService")
local dataSave = {}
function dataSave:save(plr, dataStoreName, data)
if not dataStoreName then
warn("Data store name not provided!")
return false
end
if not data then
warn("No data provided to save for player: " .. plr.Name)
return false
end
local dataStore = DataStoreService:GetDataStore(dataStoreName)
local success, err = pcall(function()
dataStore:SetAsync(plr.UserId, data)
end)
if success then
print("Data saved successfully for player: " .. plr.Name)
return true
else
warn("Error saving data: " .. tostring(err))
return false
end
end
function dataSave:load(plr, dataStoreName)
if not dataStoreName then
warn("Data store name not provided!")
return nil
end
local dataStore = DataStoreService:GetDataStore(dataStoreName)
local data
local success, err = pcall(function()
data = dataStore:GetAsync(plr.UserId)
end)
if success then
print("Data loaded successfully for player: " .. plr.Name)
return data
else
warn("Error loading data: " .. tostring(err))
return nil
end
end
return dataSave
I recommend you give this post a read, it’s very informative.
This isn’t necessarily true on it’s own, yes a common method for saving memory in storage does convert data to a string as the first step, but that is not the full process. After you convert the data to a string, it is typically compressed to a smaller size using any sort of text compression library.
Edit: You can also look into EncodingService for compressing data. It hosts a bunch of built in methods that help specifically with compressing and storing data.
Why do you need any of that and why do you use methods when they are useless?
Just have a central module that exposes player’s save and that it
Then design some binary layout per player
Never use tables or JSON slop
Always use binary layouts they are quite easy and pay off
Yes, your right for using buffers to make the code into bytes to save memory and space in the dataStore, but i thought to something easy to understand.
ZSTD is a compression algorithm, not just something Roblox does to buffers automatically. EncodingService handles the actual compression of strings, so if you are already using buffers or raw bytes, you are likely hitting diminishing returns unless your data is massive.