I'm wondering what the best way to store data in an OrderedDataStore is

Hi! I’m using OrderedDataStores for global leaderboards, but from what I’ve researched, OrderedDataStores cannot sort data when given something such as:

{
["Stats"] = {
	["Money"] = 500;
	["Gold"] = 10;
	["example"] = 0;
};

};

So I have set up a second DataStore, which is a OrderedDataStore, but I’m curious as to what the best method of saving to this is. Currently, whenever I initialize a player’s data upon joining, I start a loop that uses :SetAsync on the OrderedDataStore every 3 minutes:

delay(0, function()
	while wait() do
		if (player) and module.LoadedPlayers[player] then
			local money = module.fetchData(player, "Stats", "Money")
			MoneyOrderedStore:SetAsync(tostring(player.UserId), money)
			wait(180) -- Put here so it updates as soon as a player joins.
		else
			break
		end
	end
end)

Is there a better method of doing this? Any help is awesome, thank you.

2 Likes

This is pretty much exactly how I do it. As far as I know there is no better method just because of how datastores work, it’s really inefficient and I hate having to store two copies of the same data, but there doesn’t seem to be a way around that. The only advice I would give here, and it’s a micro optimisation, is that you might want to check if the data has changed since the last time the loop ran. If the player’s money value is the same, there’s no point in pushing it to the ODS again and using up some of your limit.

3 Likes

Well, I’m glad I wasn’t missing something super obvious. Thank you for the advice, I’ll make sure to incorporate it. :smiley:

2 Likes

I would add a pcall, and me personally would use UpdateAsync.