Skip to content

CloudStore

Type

CloudStore is a collection of records shared by every server of the game, opened with CloudStore.Open. See CloudStore for the request budget and size limits.

Each method yields until the request finishes and returns a StoreStatus first. Keys are strings of 1 to 128 printable characters. Set and Update refuse nan and infinity in numbers or vectors.

Get

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

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

Example

-- Server script.
local profiles = CloudStore.Open("profiles")
local gold: { [number]: number } = {}

Component.User.Added:Connect(function(player: Entity)
    local user = player.User
    if not user then
        return
    end
    local status, data = profiles:Get(tostring(user.Id))
    if status ~= "Ok" then
        warn("could not load profile: " .. status)
        return
    end
    gold[user.Id] = if type(data) == "table" then (data :: { gold: number }).gold else 0
end)

Set

Method
Set(key: string, value: StoreValue, options: CloudSetOptions?) -> (StoreStatus, number?)

Set saves value under key and returns (status, version). Pass { IfVersion = version } to fail with "Conflict" if another server wrote first, or { IfMissing = true } to create the key only when absent.

Example

-- Server script.
local profiles = CloudStore.Open("profiles")
local gold: { [number]: number } = {}

Component.User.Removed:Connect(function(player: Entity)
    local user = player.User
    if not user then
        return
    end
    local profile = { gold = gold[user.Id] or 0 }
    local status = profiles:Set(tostring(user.Id), profile)
    if status ~= "Ok" then
        warn("could not save profile: " .. status)
    end
end)

Update

Method
Update(key: string, fn: (value: StoreValue?, version: number?) -> StoreValue?) -> (StoreStatus, StoreValue?, number?)

Update loads a record, passes (value, version) to fn, saves the value fn returns, and returns (status, value, version), or "Cancelled", nil, nil if fn returns nil. On a version conflict, fn runs again up to 3 times.

Example

-- Server script: give each player 10 gold when they join.
local profiles = CloudStore.Open("profiles")

Component.User.Added:Connect(function(player: Entity)
    local user = player.User
    if not user then
        return
    end
    profiles:Update(tostring(user.Id), function(data)
        local gold = if type(data) == "table" then (data :: { gold: number }).gold else 0
        return { gold = gold + 10 }
    end)
end)

Remove

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

Remove deletes the record saved under key and returns (status, removed).

Example

-- Server script: delete the profile of user 42.
local profiles = CloudStore.Open("profiles")

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

ListKeys

Method
ListKeys(options: CloudListOptions?) -> (StoreStatus, { string }?)

ListKeys lists keys in key order and returns (status, keys). Pass { After = lastKey, Limit = 100 } to page through results.

Example

-- Server script.
local profiles = CloudStore.Open("profiles")

task.spawn(function()
    local after: string? = nil
    while true do
        local status, keys = profiles:ListKeys({ After = after, Limit = 100 })
        if status ~= "Ok" or not keys or #keys == 0 then
            break
        end
        for _, key in keys do
            print(key)
        end
        if #keys < 100 then
            break
        end
        after = keys[#keys]
    end
end)