Global leaderboard using Suphi's Datastore module

If it is needed, my template looks like this:

local template = {
	["Wins"] = 0,
	["Coins"] = 0
}

I want the leaderboard to track wins, I have no issue with the actual leaderboard, just the data retrieving part.

The module

local DataStoreModule = require(11671168253)
local winsOrderedDataStore = game:GetService("DataStoreService"):GetOrderedDataStore("Wins")
local coinsOrderedDataStore = game:GetService("DataStoreService"):GetOrderedDataStore("Coins")

local function Saving(value, dataStore)
    winsOrderedDataStore:SetAsync(dataStore.Key, value.Wins)
    coinsOrderedDataStore:SetAsync(dataStore.Key, value.Coins)
end

game.Players.ChildAdded:Connect(function(player)
    local dataStore = DataStoreModule.new("Player", player.UserId)
    dataStore.Saving:Connect(Saving)
end)

thats how you save there value into the ordered datastore then you simply get the order from there like you would normaly do it

you can find examples here OrderedDataStore | Documentation - Roblox Creator Hub

local winsOrderedDataStore = game:GetService("DataStoreService"):GetOrderedDataStore("Wins")

local function PrintTopTenWinsPlayers()
	local isAscending = false
	local pageSize = 10
	local pages = winsOrderedDataStore:GetSortedAsync(isAscending, pageSize)
	local topTen = pages:GetCurrentPage()

	for rank, data in topTen do
		local key = data.key
		local wins = data.value
		print(key, "is ranked #", rank, "with", wins, "wins")
	end
end

but you should never use the orderedDataStore datastore as the source of truth its only for display purposes always use dataStore.Value.Wins as the source of truth

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.