Skip to content

SortedCloudStore

Type

SortedCloudStore is a shared leaderboard of whole-number scores opened with CloudStore.OpenSorted; see CloudStore for request budgets. Keys are strings of 1 to 128 printable characters, and scores are whole numbers strictly within (-2^53, 2^53).

Every method yields until the request finishes and returns a StoreStatus first. A local test server keeps scores in memory, so they are gone when it stops.

Set

Method
Set(key: string, score: number) -> StoreStatus

Set saves a whole-number score under key and returns a StoreStatus.

Example

-- Server script.
local wins = CloudStore.OpenSorted("total_wins")

Component.User.Added:Connect(function(player: Entity)
    local user = player.User
    if not user then
        return
    end
    local status = wins:Set(tostring(user.Id), 1)
    if status ~= "Ok" then
        warn(status)
    end
end)

Get

Method
Get(key: string) -> (StoreStatus, number?)

Get loads the score saved under key and returns (status, score). A missing key returns "Ok", nil.

Example

-- Server script.
local wins = CloudStore.OpenSorted("total_wins")

task.spawn(function()
    local status, score = wins:Get("1001")
    if status == "Ok" then
        print(score or 0)
    end
end)

GetSorted

Method
GetSorted(options: SortedOptions?) -> (StoreStatus, { SortedEntry }?)

GetSorted loads up to Limit (1 to 100, default 100) { Key, Score } entries sorted by score (Order = "Descending" by default, or "Ascending") and returns (status, entries). Optional Min, Max, and After fields bound the score range or continue after the last entry of the previous page.

Example

-- Server script.
local wins = CloudStore.OpenSorted("total_wins")

task.spawn(function()
    local status, entries = wins:GetSorted({ Order = "Descending", Limit = 10 })
    if status ~= "Ok" or not entries then
        warn(status)
        return
    end
    for rank, entry in entries do
        print(rank, entry.Key, entry.Score)
    end
end)

Remove

Method
Remove(key: string) -> (StoreStatus, boolean?)

Remove deletes key from the sorted store and returns (status, removed).

Example

-- Server script.
local wins = CloudStore.OpenSorted("total_wins")

task.spawn(function()
    local status, removed = wins:Remove("1001")
    print(status, removed)
end)

Increment

Method
Increment(key: string, delta: number) -> (StoreStatus, number?)

Increment adds whole-number delta to key atomically (starting from 0 if absent) and returns (status, score). If the new score would reach 2^53 in magnitude, Increment returns "TooLarge", nil.

Example

-- Server script.
local wins = CloudStore.OpenSorted("total_wins")

task.spawn(function()
    local status, score = wins:Increment("1001", 1)
    if status == "Ok" then
        print(score)
    end
end)