学习
引擎类
MemoryStoreSortedMap
无法创建
未复制

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处


概要
方法
GetRangeAsync(direction: Enum.SortDirection,count: number,exclusiveLowerBound: Variant,exclusiveUpperBound: Variant):{any}
SetAsync(key: string,value: Variant,expiration: number,sortKey: Variant):boolean
UpdateAsync(key: string,transformFunction: function,expiration: number):Tuple
继承成员

API 参考
方法
GetAsync
暂停
功能: DataStore
MemoryStoreSortedMap:GetAsync(key:string):Tuple
参数
key:string
返回

GetRangeAsync
暂停
功能: DataStore
MemoryStoreSortedMap:GetRangeAsync(
direction:Enum.SortDirection, count:number, exclusiveLowerBound:Variant, exclusiveUpperBound:Variant
参数
count:number
exclusiveLowerBound:Variant
exclusiveUpperBound:Variant
返回
代码示例
检索 MemoryStore 键
local MemoryStoreService = game:GetService("MemoryStoreService")
local myMap = MemoryStoreService:GetSortedMap("MySortedMap")
function printAllKeys(map)
-- 初始下限为 nil,这意味着从第一个项目开始
local exclusiveLowerBound = nil
-- 这个循环一直持续到地图的末尾
while true do
-- 从当前下限开始获取最多一百个项目
local items = map:GetRangeAsync(Enum.SortDirection.Ascending, 100, exclusiveLowerBound)
for _, item in ipairs(items) do
print(item.key)
print(item.sortKey)
end
-- 如果返回的项目少于一百个,则表示我们已经到达地图的末尾
if #items < 100 then
break
end
-- 最后检索到的键是下一次迭代的排他性下限
exclusiveLowerBound = {}
exclusiveLowerBound["key"] = items[#items].key
exclusiveLowerBound["sortKey"] = items[#items].sortKey
end
end
printAllKeys(myMap)

GetSizeAsync
暂停
功能: DataStore
MemoryStoreSortedMap:GetSizeAsync():number
返回

RemoveAsync
暂停
功能: DataStore
MemoryStoreSortedMap:RemoveAsync(key:string):()
参数
key:string
返回
()

SetAsync
暂停
功能: DataStore
MemoryStoreSortedMap:SetAsync(
key:string, value:Variant, expiration:number, sortKey:Variant
参数
key:string
value:Variant
expiration:number
sortKey:Variant
返回

UpdateAsync
暂停
功能: DataStore
MemoryStoreSortedMap:UpdateAsync(
key:string, transformFunction:function, expiration:number
参数
key:string
transformFunction:function
expiration:number
返回
代码示例
更新内存存储排序地图
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` 是用于对地图中的项进行排序的 sortKey
playerScore = playerStats.kills / math.max(playerStats.deaths, 1)
return playerStats, playerScore
end
return nil
end, 30)
end)
end
-- 为所有玩家增加一次击杀
for i, player in pairs(Players:GetPlayers()) do
updateLeaderboard(player.UserId, 1, 0)
end
-- 为 userId 为 12345 的玩家增加 5 次击杀和 1 次死亡
updateLeaderboard(12345, 5, 1)

©2026 Roblox Corporation、Roblox、Roblox 标志及 Powering Imagination 是我们在美国及其他国家或地区的注册与未注册商标。