MemoryStoreSortedMap
Show Deprecated
Summary
Methods
- GetRangeAsync(direction : Enum.SortDirection,count : number,exclusiveLowerBound : Variant,exclusiveUpperBound : Variant):Array
Properties
Methods
GetRangeAsync
Parameters
Default Value: ""
Default Value: ""
exclusiveLowerBound: Variant
Default Value: ""
exclusiveUpperBound: Variant
Default Value: ""
Returns
Code Samples
Retrieving MemoryStore Keys
local MemoryStoreService = game:GetService("MemoryStoreService")
local myMap = MemoryStoreService:GetSortedMap("MySortedMap")
function printAllKeys(map)
-- the initial lower bound is nil which means to start from the very first item
local exclusiveLowerBound = nil
-- this loop continues until the end of the map is reached
while true do
-- get up to a hundred items starting from the current lower bound
local items = map:GetRangeAsync(Enum.SortDirection.Ascending, 100, exclusiveLowerBound)
for _, item in ipairs(items) do
print(item.key)
print(item.sortKey)
end
-- if the call returned less than a hundred items it means we've reached the end of the map
if #items < 100 then
break
end
-- the last retrieved key is the exclusive lower bound for the next iteration
exclusiveLowerBound = {}
exclusiveLowerBound["key"] = items[#items].key
exclusiveLowerBound["sortKey"] = items[#items].sortKey
end
end
printAllKeys(myMap)
SetAsync
Parameters
Default Value: ""
value: Variant
Default Value: ""
Default Value: ""
sortKey: Variant
Default Value: ""
Returns
UpdateAsync
Parameters
Default Value: ""
Default Value: ""
Default Value: ""
Returns
Code Samples
Updating a MemoryStore Sorted Map
local MemoryStoreService = game:GetService("MemoryStoreService")
local map = MemoryStoreService:GetSortedMap("Leaderboard")
local Players = game:GetService("Players")
function updateLeaderboard(itemKey, killsToAdd, deathsToAdd)
local success, newStats, newScore = pcall(function()
return map: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)
end
-- Add one kill to all players
for i, player in pairs(Players:GetPlayers()) do
updateLeaderboard(player.UserId, 1, 0)
end
-- Add 5 kills and 1 death to player with userId 12345
updateLeaderboard(12345, 5, 1)