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 callbackboolean, string, number)
Get the latest value with the provided key, and call the callback function when the Get operation is completed
keyThe key for which the value is requestedcallbackThe callback function to be executed when the Get operation is completedisSuccessThis parameter indicates if the request has been successfulmessageIf the request has not been successful, you will get an error messagevalueThe value in the orderedDatastore for the given key
-- 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 callbackboolean, string, number)
Set the latest value for the provided key, and cal the callback function when the Set operation is completed
keyThe key for which the value is requested.valueThe value will be set to.callbackThe callback function to be executed when the Set operation is completedisSuccessThis parameter indicates if the Set operation has been successfulmessageIf the Set operation has not been successful, you will get an error messagevalueThe latest value in the orderedDatastore for the given key
-- 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 callbackboolean, string, number)
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
keyThe key for which the value should be updateddeltaThe amount to be added to the current valuecallbackThe callback function to be executed when the Increment operation is completedisSuccessThis parameter indicates if the addition has been successfulmessageIf the addition has not been successful, you will get an error messagevalueThe latest value in the orderedDatastore for the given key
-- 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 callbackboolean, string, number)
Remove the value permanently, and call the callback function when the Remove operation is completed
keyThe key for which the value should be removedcallbackThe callback function to be executed when the Remove operation is completedisSuccessThis parameter indicates if the Remove operation has been successfulmessageIf the Remove operation has not been successful, you will get an error messagevalueThe value in the datastore prior to the Remove operation
-- 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 callbackboolean, string, ..., [number minValue], [number maxValue])
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
ascendingWhether the pages are sorted in ascending orderpageSizeThe length of pagescallbackThe callback function to be executed when the Get operation is completedisSuccessThis parameter indicates if the Get operation has been successfulmessageIf the Get operation has not been successful, you will an get error messageOrderedDatastorePagesThe resulting OrderedDatastorePages
minValueOptional parameter. If set, data pages with a value less than than minValue will be excludedmaxValueOptional parameter. If set, data pages with a value greater than maxValue will be excluded
-- 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)