Custom leaderboards with OrderedDataStore: what's different from DataStore

If you want a leaderboard with your own UI (best score, total kills, fastest clear), OrderedDataStore is the tool. It behaves a bit differently from regular DataStore though, and a few of those differences will trip you up if you assume it’s the same thing with sorting bolted on.

Integers only

Decimals, strings, bools, tables: all rejected. Saving one raises an error and keeps the old value. NaN and Inf are rejected too.

For times like 12.34s, scale and store as an integer:

local saveValue = math.floor(clearTime * 100) -- 1234
local displayTime = saveValue / 100           -- 12.34

For fastest-time boards, remember to sort ascending (lower is better).

Use UpdateAsync for “best score” boards

SetAsync will happily overwrite a 5000 with a 300. If the rule is “only save if it beats the current record”, do the check inside UpdateAsync so it’s made against the latest value even when multiple servers write at once:

local function SaveBestScore(player, score)
    local ok, result = pcall(function()
        return LeaderboardStore:UpdateAsync(player.UserId, function(currentBest)
            if currentBest ~= nil and currentBest >= score then
                return nil -- cancel the write
            end
            return score
        end)
    end)

    if not ok then
        warn("SaveBestScore failed: " .. tostring(result))
    elseif result == nil then
        -- existing record was higher, nothing saved
    end
end

Heads up if you’re used to regular DataStore: here the callback gets only the current value (no KeyInfo), and you return the integer directly. No userIds / metadata.

For “total” boards (kills, wins), IncrementAsync(key, delta) is simpler. If the key doesn’t exist yet, delta becomes the starting value.

Don’t query rankings per client

Ranking retrieval has the narrowest request limit of the bunch. The pattern from the docs is: the server calls GetSortedAsync on an interval (e.g. every 60s), caches the top N, and pushes it to clients with FireAllClients. Clients never trigger the query themselves.

Also both arguments to GetSortedAsync(ascending, pagesize) are required, and pagesize has to be 1-100.

local ok, pages = pcall(function()
    return LeaderboardStore:GetSortedAsync(false, 10)
end)
if ok then
    for rank, entry in ipairs(pages:GetCurrentPage()) do
        print(rank, entry.key, entry.value)
    end
end

An empty board returns an empty table, not nil, so the loop is safe as-is.

Seasons with scope

Instead of renaming your store every season, pass a scope:

local Season1 = DataStoreService:GetOrderedDataStore("KillLeaderboard", "Season1")
local Season2 = DataStoreService:GetOrderedDataStore("KillLeaderboard", "Season2")

Old seasons stay readable if you ever want a hall of fame.

Things that are easy to forget

  • No version history. Overwrites and deletes are permanent.
  • Names, scopes and keys are case sensitive. KillLeaderboard and killLeaderboard are two different boards.
  • GetDataStore("Score") and GetOrderedDataStore("Score") are separate stores even with the same name.
  • Server scripts only. Calling from the client errors.
  • If you just want rankings shown in the app’s Top Scorer section and above characters, that’s WorldRankService, not this.

Source: Custom Leaderboard

What kind of boards are people running? I’m wondering whether weekly resets actually bring players back more than all-time boards do.