Hi Developers,
We are excited to announce the Sorted Map Data Structure in the MemoryStore Service now supports sorting the items with a special field, in addition to the key! This feature is fully backwards compatible and will not disrupt your existing usage of MemoryStore Sorted Maps.
We understand that for certain use cases, such as leaderboards, you would like to have the ability to sort items in your Sorted Map by something other than the key. While this can be done by prefixing the key with a value, you still need to think about cases such as padding a number with leading 0s.
With this new sort key field, the data in your Sorted Map will be sorted using this optionally supplied sort key. Some details on this parameter:
- The sort key can be up to 128 bytes in size
- It can be a string or a number (integer or floating point)
Items with a sort key will sort before items without a sort key. Numeric sort keys will take precedence over items with string sort keys.
The existing APIs supported for the Sorted Map data structure have been updated to support this new field.
-
GetAsync
: Returns sort key (if present) along with value -
SetAsync
: Accepts an optional sort key along with key, value and expiration -
UpdateAsync
: In addition to the value, provides the sort key to the transform callback and accepts the sort key from the transform callback -
GetRangeAsync
: Accepts a Lua table as a boundary instead of a string key, where one or both of key or sort key are provided in the following format
{
sortKey: Variant,
key: string,
}
Each returned item will have a sort key (if present) along with the value.
For example, here are some items in a sorted map in ascending order
{Key: "player1", Value: v, SortKey: -1} -- negative sort key
{Key: "player2", Value: v, SortKey: 0} -- 0 sort key
{Key: "player4", Value: v, SortKey: 1} -- 1 sort key, key "player4"
{Key: "player5", Value: v, SortKey: 1} -- 1 sort key, key "player5"
{Key: "player3", Value: v, SortKey: 3.14} -- 3.14 sort key
{Key: "player6", Value: v, SortKey: "someString"} -- "someString" sort key
{Key: "player0", Value: v} -- no sort key, key "player0"
{Key: "player7", Value: v} -- no sort key, key "player7"
Here is an example of how this feature can be used to build a leaderboard:
-- Updating the leaderboard score for a player in a Sorted Map
local MemoryStoreService = game:GetService("MemoryStoreService")
local sortedMap = MemoryStoreService:GetSortedMap("Leaderboard")
local function updateLeaderboard(itemKey, killsToAdd, deathsToAdd)
local score = killsToAdd / math.max(deathsToAdd, 1)
local success, newStats, newScore = pcall(function()
return sortedMap:UpdateAsync(itemKey, function(playerStats, playerScore)
playerStats = playerStats or { kills = 0, deaths = 0 }
playerStats.kills += killsToAdd
playerStats.deaths += deathsToAdd
if playerStats then
-- `playerScore` is the sortKey being used to sort items in the map
playerScore = playerStats.kills / math.max(playerStats.deaths, 1)
return playerStats, playerScore
end
return nil
end, 30)
end)
if success then
print(newStats)
print(newScore)
end
end
To learn more about this new field, check out the new API documentation and updated API guide available now.
As always, your feedback is invaluable for us to improve our services and fit your needs. Feel free to share any comments or questions below, and we’ll do our best to answer. We can’t wait to see what you build with this new feature!
Happy building!
The Roblox Creator Services Team