UpdateAsync or SetAsync for leaderboards?

So right now im making a leaderboard and how its going to work is every 2 mins or so its going to update
every players win value if it has been changed or not so I don’t waste any calls to the datastore but I’m wondering for setting the players value should I use UpdateAsync or SetAsync i mean if im going to use UpdateAsync i would need to use the old value it passes but is there any checks i would use? I mean I’m already only updating the players that had there value changed just wondering since I haven’t found anything like this said "for leaderboards" only normal player data but just want your thought on how this should go

– this is how its going to work

local LBS = {
	WinsLeaderBoard = DS:GetOrderedDataStore(MainKey.."WINS")
}
--

local DATA = {}

local DATA_TO_SAVE = {
	WINS = {}
	
	
}
local function UpdateLeaderBoard()
	local PAGES={}
	local s , e = pcall(function()
		for i ,v in pairs(LBS) do
			local data = v:GetSortedAsync(false,PAGE_SIZE)
			PAGES[i]=data:GetCurrentPage()
		end
	end)
	if s then
		table.clear(DATA)
		for i ,v in pairs(PAGES) do
			DATA[i]=v
		end
		UPDATE:FireAllClients(DATA)
	end
	for TYPE , TABLE in pairs(DATA_TO_SAVE) do
		for PLAYER , VALUE in pairs(TABLE) do
			-- save here	
		end
	end
	
end

I’ll always use SetAsync(), so I recommend it for a better experience, no error or bug, etc.

1 Like

i guess so since im only adding it to the table when the value changes but im just still wondering if i can any checks inside the UpdateAsync function

UpdateAsync, it gives you a chance to validate that the data you’re attempting to write is not lower than or equivalent to the existing value. SetAsync doesn’t give you that chance.

:UpdateAsync(key_name, function(currentScore)
    -- If newScore <= currentScore return nil to cancel the write
end)

Only real benefit is the validation. If you use SetAsync and expect multiple servers to write to one key then they can encounter race conditions and apply the wrong data.

3 Likes

Yeah i was thinking about an hour ago i would just check if its less then the value it wants to save then just cancel good point, im also only going to save players value that have been changed, in a queue not actually saving every time it changes

just a side note i know the call might take a bit longer then SetAsync since its grabbing and saving at the same time but thanks!,

im just going to go with this since its the safest way to prevent the under value issue