Hello!
Essentially looking for feedback here.
So, I have a datastore that saves every single time a player leaves and joins, but then I have a datastore (daily rewards) that should only save conditionally. I’ll show my datastore script here, as well as comment on what everything does:
local function unloadData(tbl) -- packs everything into a table which will be the direct "child" of the table that was passed as an argument from the function calling it
local returnedData = {}
for i,v in pairs(tbl:GetChildren()) do
if v:IsA('ValueBase') then
returnedData = v.Value
elseif v:IsA('Folder') then
if #v:GetChildren() == 0 then
returnedData[v.Name] = {}
else
unloadData(v) -- calls itself if it's a nested folder
end
end
end
return returnedData
end
game.Players.PlayerRemoving:Connect(function(plr) -- packs everything into the 'base' table local key = 'DATA_'..plr.UserId
local compiledData = {}
for i,v in pairs(plr:FindFirstChild('PlayerData'):GetChildren()) do
if v:IsA('ValueBase') then
compiledData[v.Name] = v.Value
elseif v:IsA('Folder') then
compiledData[v.Name] = unloadData(v)
end
end
local data = HttpService:JSONEncode(compiledData)
local s,e = pcall(function()
DataStore:SetAsync(key, data)
end)
if not s then
warn('Unable to set data for '..tostring(plr)..'. '..tostring(e))
end
end)
Basically, should I just add an if statement to check if it’s the os.time() value, or should I do that in a separate datastore?