Creating a separate datastore for daily rewards, yes or no?

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?

I would keep everything as simple as possible. Have a central data table and just store LastTimeCollected (os.time() or os.date()) in that table. It keeps things neat and organized.

2 Likes

Yep, that’s essentially what I have. I selected the value that stores the last saved value. Problem is that everything except for the LastLogin saves automatically. LastLogin should only save conditionally, basically if the player logs in and 24 hours have passed since the last login. I’m having a dilemma on whether I should create a separate datastore for this one conditionally saving value, or should I just add an elseif to check if the name of the value is ‘LastLogin’?

Capture d’écran, le 2020-11-12 à 18.41.22

When you save the rest of the values, just verify the last login, and then update it if you need.

2 Likes