Getting the Number of Items in a Sorted Map (MemoryStoreService)

As a Roblox developer, it is currently too hard to get the number of items/keys within a sorted map using MemoryStoreService. Currently, the only way to do so would be to run a function like the one below that iterates through the entire sorted map:

local MemoryStoreService = game:GetService("MemoryStoreService")
local Map = MemoryStoreService:GetSortedMap("map_name")

local page = {}
local startFrom = nil
local items = 0

repeat
    page = Map:GetRangeAsync(Enum.SortDirection.Ascending, 200, startFrom)
    local count = #page

    if count > 0 then
        items += count
        startFrom = page[count].key
    end
until #page == 0

print(items) -- prints the number of items within our map

This method is inefficient and costly when abiding by the Memory Store API Request Limits.

Therefore I propose either a new property or method be added to the MemoryStoreSortedMap class that allows the developer to read the number of items within a sorted map. This functionality may also be useful for the MemoryStoreQueue class as well.

Here are some possible use cases for this feature:

  • Display to the user how many players are currently matchmaking.
  • Display to the user how many items are in an auction.
  • Limit how many items can be in an auction (ex. if the map count is greater than x then don’t allow any more items to be added)
  • For some sort of temporary posting system, limit the total amount of posts to <x. Or, separate into categories if posts >x.
5 Likes