Skip to content

OrderedDatastore

OrderedDatastore is essentially a Datastore that stores and organizes numeric values. It exposes a method GetSortedAsync(), which allows users to inspect entries in sorted order using a OrderedDatastorePages object. This OrderedDatastore struct contains a set of functions for manipulating the stored data.

Functions

void GetAsync(string key, function callback<boolean, string, number>)

SERVER ONLY 
Get the latest value with the provided key, and call the callback function when the Get operation is completed
  • key The key for which the value is requested
  • callback The callback function to be executed when the Get operation is completed
    1. isSuccess This parameter indicates if the request has been successful
    2. message If the request has not been successful, you will get an error message
    3. value The value in the orderedDatastore for the given key
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
 -- example GetAsync
 local orderedDatastore = YaDatastoreAPI.GetOrderedDatastore("leaderboard")

 local key = "testKey"
 local function callback(isSuccess,message,value)
     print("whether request is success:",isSuccess)
     if isSuccess then
         print("value:",value)
     else
         print("error message:",message)
     end
 end

 orderedDatastore:GetAsync(key, callback)

void SetAsync(string key, number value, function callback<boolean, string, number>)

SERVER ONLY 
Set the latest value for the provided key, and cal the callback function when the Set operation is completed
  • key The key for which the value is requested.
  • value The value will be set to.
  • callback The callback function to be executed when the Set operation is completed
    1. isSuccess This parameter indicates if the Set operation has been successful
    2. message If the Set operation has not been successful, you will get an error message
    3. value The latest value in the orderedDatastore for the given key
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
 -- example SetAsync
 local orderedDatastore = YaDatastoreAPI.GetOrderedDatastore("leaderboard")

 local key = "testKey"
 local value = 100
 local function callback(isSuccess,message,value)
     print("whether request is success:",isSuccess)
     if isSuccess then
         print("value:",value)
     else
         print("error message:",message)
     end
 end

 orderedDatastore:SetAsync(key, value, callback)

void IncAsync(string key, number delta, function callback<boolean, string, number>)

SERVER ONLY 
Increment the value for a key by the provided amount (both must be integers), and call the callback function when the Increment operation is completed
  • key The key for which the value should be updated
  • delta The amount to be added to the current value
  • callback The callback function to be executed when the Increment operation is completed
    1. isSuccess This parameter indicates if the addition has been successful
    2. message If the addition has not been successful, you will get an error message
    3. value The latest value in the orderedDatastore for the given key
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
 -- example IncAsync
 local orderedDatastore = YaDatastoreAPI.GetOrderedDatastore("leaderboard")

 local key = "testKey"
 local delta = 20
 local function callback(isSuccess,message,value)
     print("whether request is success:",isSuccess)
     if isSuccess then
         print("value:",value)
     else
         print("error message:",message)
     end
 end

 orderedDatastore:IncAsync(key, delta, callback)

void RemoveAsync(string key, function callback<boolean, string, number>)

SERVER ONLY 
Remove the value permanently, and call the callback function when the Remove operation is completed
  • key The key for which the value should be removed
  • callback The callback function to be executed when the Remove operation is completed
    1. isSuccess This parameter indicates if the Remove operation has been successful
    2. message If the Remove operation has not been successful, you will get an error message
    3. value The value in the datastore prior to the Remove operation
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
 -- example RemoveAsync
 local orderedDatastore = YaDatastoreAPI.GetOrderedDatastore("leaderboard")

 local key = "testKey"
 local function callback(isSuccess,message,value)
     print("whether request is success:",isSuccess)
     if isSuccess then
         print("value:",value)
     else
         print("error message:",message)
     end
 end

 orderedDatastore:RemoveAsync(key, callback)

void GetSortedAsync(boolean ascending, number pageSize, function callback<boolean, string, ...>, [number minValue], [number maxValue])

SERVER ONLY 
Get the OrderedDatastorePages, and call the callback function when the Get operation is completed It displays a certain range of data sorted in ascending order and the length limited by the provided pageSize It also displays minValue/maxValue, which are optional parameters that filter the results
  • ascending Whether the pages are sorted in ascending order
  • pageSize The length of pages
  • callback The callback function to be executed when the Get operation is completed
    1. isSuccess This parameter indicates if the Get operation has been successful
    2. message If the Get operation has not been successful, you will an get error message
    3. OrderedDatastorePages The resulting OrderedDatastorePages
  • minValue Optional parameter. If set, data pages with a value less than than minValue will be excluded
  • maxValue Optional parameter. If set, data pages with a value greater than maxValue will be excluded
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
 -- example GetSortedAsync
 local orderedDatastore = YaDatastoreAPI.GetOrderedDatastore("leaderboard")

 local isAscending = false
 local pageSize = 20
 local function callback(isSuccess,message,result)
     print("whether request is success:",isSuccess)
     if isSuccess then
 -- The result is a type of userdata, indexed starting from 0
         for i,t in ipairs(pages) do
             local name = t.Key
             local points = t.Value
             print(name .. " is top " .. i .. " with " .. points .. "points")
         end
     else
         print("error message:",message)
     end
 end
 orderedDatastore:GetSortedAsync(isAscending, pageSize, callback)