Codectionary / Developer documentation / Luau

OrderedDataStore leaderboards

Read ranked numeric values for a leaderboard.

Why use OrderedDataStore?

OrderedDataStore supports sorted numeric data across servers. It is useful for leaderboards, while each request is subject to service limits and can fail; the main DataStore guide still applies to saving authoritative player data.

Try it in Roblox Studio

Publish the place and enable Studio API access for a private test place if testing persistence in Studio. Put this Script in ServerScriptService; the data store must already contain numeric scores to show results.

Syntax

local pages = store:GetSortedAsync(false, 5)

Examples

OrderedDataStore leaderboards

Read up to five highest stored scores.

local DataStoreService = game:GetService("DataStoreService")
local scores = DataStoreService:GetOrderedDataStore("PracticeScores")
local ok, pages = pcall(function()
    return scores:GetSortedAsync(false, 5)
end)
if not ok then
    warn("Leaderboard unavailable")
else
    for _, entry in pages:GetCurrentPage() do
        print(entry.key, entry.value)
    end
end
  1. GetOrderedDataStore selects a named sorted store; this snippet reads but does not write scores.
  2. GetSortedAsync(false, 5) asks for a descending page of up to five entries.
  3. pcall handles request failures without crashing the Script. A successful empty page means no matching scores have been stored.

Best practices

  • Handle throttling and failures; never assume a leaderboard request succeeds.
  • Store only server-validated numeric scores in the ordered store.

Runtime & versions

Luau host; Roblox engine examples require Roblox Studio. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

Luau documentation

Specifications & further reading