KeyValueStore

KeyValueStores can be used to store and retrieve user data.

You can use several KeyValueStores, they will all be scoped to your World.

For example, you can use a [Player.UserID] to store data that belongs to a one Player:

Server.DidReceiveEvent = function(event)
  
  if event.action == "getXP" then

    -- retrieve and return player's experience:
    local store = KeyValueStore(event.Sender.UserID) -- use UserID as store name

    -- get value for "xp" key
    store:Get("xp", function(success, results)
        if success then
          local response = Event()
          response.xp = results.xp
          response:SendTo(event.Sender)
        end
    end)
  end
end

⚠️ Currently, KeyValueStores can only be used by the Server. You will need to use Events to ask the Server to store and retrieve data. It's a bit cumbersome, but it will be possible to use this from Clients too real soon.

⚠️ It's possible to save high scores (sorted data) using a KeyValueStore, but SortedSets (coming soon) will be a better option for this.

Constructors

KeyValueStore ( string storeName )

Creates a KeyValueStore with given store name.

Several stores can be used in one single World, it's useful to scope your data.

local store = KeyValueStore("settings")
store:Get("currentChallenge", "jumpStrength", function(success, results)
    if success then
      -- do something with results.currentChallenge
      -- and results.jumpStrength
    end
end)

Functions

nil Get ( string key1, string key2, ... , function callback )

Gets values for given keys.

Though the operation is supposed to be real quick, it can't be instantaneous. That's why you need to supply a callback function to get the response.

The callback function only takes 2 arguments:
- A boolean, indicating if the operation was successful (it could fail because of network issues).
- A table containing values for requested keys.

local store = KeyValueStore("settings")
store:Get("currentChallenge", "jumpStrength", function(success, results)
    if success then
      -- do something with results.currentChallenge
      -- and results.jumpStrength
    end
end)
nil Set ( string key1, string value, ... , function callback optional )
nil Set ( string key1, number value, ... , function callback optional )
nil Set ( string key1, boolean value, ... , function callback optional )

Sets values for given keys.

Currently, values can be strings, numbers or booleans. More types are going to be supported soon.

Though the operation is supposed to be real quick, it can't be instantaneous. That's why you need to supply a callback function to get the response.

The callback function only takes one argument:
- a boolean, indicating if the operation was successful (it could fail because of network issues).

local store = KeyValueStore("settings")
store:Set("currentChallenge", "halloween", "jumpStrength", 10, function(success)
    if success then
      -- operation was successful
    end
end)

Properties

string Name read-only

KeyValueStore's name.