Making global leaderboard with DataStore2

Hello, I want to achieve a global leaderboard with DataStore2, I know how to make one with normal DS via this tutorial;

I just have no idea how to customize it to work with DS2.

Does anyone have some experience with this and could help me?
Thanks in advance.

2 Likes

You simply can’t do this directly with DataStore2. It’s easier to create a separate OrderedDataStore and just save the value there whenever DataStore2 updates.

You can use the AfterSave method on your DataStore2 object to save the respective data to the player:

local store = DataStore2("Data", player)

store:AfterSave(function(newData)
	repeat
		local s = pcall(orderedStore.SetAsync, orderedStore, player.UserId, newData)
	until s
end)

With this in place, you can just use GetSortedAsync on orderedStore to get the leaderboard you need.

9 Likes

Thank you, but then, I would have to call Save() every x minuets or it gets called every time it gets :Incremented or :Set?

1 Like

:Save() is automatically called when the Player is leaving, however I’d recommend to call it every 2-3 minutes.

4 Likes