Impara
Classe motore
MemoryStoreHashMap
Non costruibile
Non Replicato

*Questo contenuto è tradotto usando AI (Beta) e potrebbe contenere errori. Per visualizzare questa pagina in inglese, clicca qui.


Sommario
Metodi
GetAsync(key: string):Variant
SetAsync(key: string,value: Variant,expiration: number):boolean
UpdateAsync(key: string,transformFunction: function,expiration: number):Variant
Membri ereditati

Riferimento API
Metodi
GetAsync
Resa
Capacità: DataStore
MemoryStoreHashMap:GetAsync(key:string):Variant
Parametri
key:string
Restituzioni
Variant
Campioni di codice
Ottenere dati da una Hash Map MemoryStore
local MemoryStoreService = game:GetService("MemoryStoreService")
local hashMap = MemoryStoreService:GetHashMap("HashMap1")
local key = "User_1234"
local value = 1000
local expiration = 30
local setSuccess, _ = pcall(function()
return hashMap:SetAsync(key, value, expiration)
end)
if setSuccess then
print("Impostazione riuscita!")
end
local item
local getSuccess, getError = pcall(function()
item = hashMap:GetAsync(key)
end)
if getSuccess then
print(item)
else
warn(getError)
end

ListItemsAsync
Resa
Capacità: DataStore
MemoryStoreHashMap:ListItemsAsync(count:number):MemoryStoreHashMapPages
Parametri
count:number
Campioni di codice
Elenco degli oggetti in una MemoryStore Hash Map
local MemoryStoreService = game:GetService("MemoryStoreService")
local testHashMap = MemoryStoreService:GetHashMap("HashMap1")
local EXPIRATION = 600
local NUM_TEST_ITEMS = 32
local function populateHashMap(hashMap: MemoryStoreHashMap, numItems: number): { [string]: any }
print("Impostando i dati della HashMap...")
local createdItems = {}
for index = 1, numItems do
local key = tostring(index) -- Le chiavi della HashMap devono essere stringhe
local value = `{key}_test_value`
local success, result = pcall(hashMap.SetAsync, hashMap, key, value, EXPIRATION)
if success then
createdItems[key] = value
else
warn(`Errore impostando la chiave {key}: {result}`)
end
end
print("Impostazione dei dati della HashMap completata.")
return createdItems
end
local function getItemsFromAllPages(pages: MemoryStoreHashMapPages): { [string]: any }
-- Puramente a scopo di registrazione, monitoriamo a quale numero di pagina siamo
local currentPageNumber = 1
local retrievedItems = {}
while not pages.IsFinished do
print(`Recuperando gli elementi della pagina {currentPageNumber}...`)
local items = pages:GetCurrentPage()
for _, entry in pairs(items) do
print(` {entry.key}: {entry.value}`)
retrievedItems[entry.key] = entry.value
end
-- Avanza le pagine se ci sono più pagine da leggere
if not pages.IsFinished then
pages:AdvanceToNextPageAsync()
currentPageNumber += 1
end
end
print("Lettura di tutte le pagine completata")
return retrievedItems
end
local function compareAllItems(retrievedItems: { [string]: any }, expectedItems: { [string]: any }): number
print("Confrontando gli elementi recuperati con quelli attesi...")
local numMatchingItems = 0
for key, expectedValue in pairs(expectedItems) do
if retrievedItems[key] == expectedValue then
numMatchingItems += 1
else
warn(`Valore recuperato non corrispondente per la chiave {key}: atteso {expectedValue}, recuperato {retrievedItems[key]}`)
end
end
print("Confronto completato!")
return numMatchingItems
end
-- Le chiavi aggiunte alla hashmap sono anche aggiunte a questa tabella expectedItems.
-- In seguito, gli elementi recuperati della hashmap verranno confrontati con questa tabella di elementi attesi.
local expectedItems = populateHashMap(testHashMap, NUM_TEST_ITEMS)
-- Ottenere le pagine può dare errore. In questo caso, permetteremo che dia errore e fermeremo l'esecuzione del programma,
-- ma potresti voler utilizzare pcall e gestirlo diversamente.
print(`Recuperando le pagine della HashMap con ListItemsAsync...`)
local pages = testHashMap:ListItemsAsync(NUM_TEST_ITEMS)
local retrievedItems = getItemsFromAllPages(pages)
local numMatchingItems = compareAllItems(retrievedItems, expectedItems)
-- Se non ci sono stati errori nell'impostare o ottenere gli elementi, tutti gli elementi dovrebbero corrispondere.
print(`Programma completato. {numMatchingItems}/{NUM_TEST_ITEMS} elementi recuperati corrispondono ai valori attesi.`)

RemoveAsync
Resa
Capacità: DataStore
MemoryStoreHashMap:RemoveAsync(key:string):()
Parametri
key:string
Restituzioni
()
Campioni di codice
Rimuovere dati da una mappa Hash di MemoryStore
local MemoryStoreService = game:GetService("MemoryStoreService")
local hashMap = MemoryStoreService:GetHashMap("HashMap1")
local key = "User_1234"
local value = 1000
local expiration = 30
local setSuccess, setError = pcall(function()
return hashMap:SetAsync(key, value, expiration)
end)
if not setSuccess then
warn(setError)
end
local removeSuccess, removeError = pcall(function()
hashMap:RemoveAsync(key)
end)
if removeSuccess then
print("Rimozione riuscita!")
else
warn(removeError)
end

SetAsync
Resa
Capacità: DataStore
MemoryStoreHashMap:SetAsync(
key:string, value:Variant, expiration:number
Parametri
key:string
value:Variant
expiration:number
Restituzioni
Campioni di codice
Aggiungere dati a una MemoryStore Hash Map
local MemoryStoreService = game:GetService("MemoryStoreService")
local hashMap = MemoryStoreService:GetHashMap("HashMap1")
local key = "User_1234"
local value = 1000
local expiration = 30
local setSuccess, setError = pcall(function()
return hashMap:SetAsync(key, value, expiration)
end)
if setSuccess then
print("Impostazione riuscita!")
else
warn(setError)
end

UpdateAsync
Resa
Capacità: DataStore
MemoryStoreHashMap:UpdateAsync(
key:string, transformFunction:function, expiration:number
):Variant
Parametri
key:string
transformFunction:function
expiration:number
Restituzioni
Variant
Campioni di codice
Aggiornamento di una Mappa Hash di Memory Store
local MemoryStoreService = game:GetService("MemoryStoreService")
local hashMap = MemoryStoreService:GetHashMap("ResourceInventory")
local function contributeResources(itemResource, addedCount)
local success, newResourceCount = pcall(function()
return hashMap:UpdateAsync(itemResource, function(resource)
resource = resource or { count = 0 }
resource.count = resource.count + addedCount
-- assicurati di non superare il conteggio massimo di risorse
if resource.count > 500 then
resource.count = 500
end
return resource
end, 1200)
end)
if success then
print(newResourceCount)
end
end
contributeResources("myResource", 50)

© 2026 Roblox Corporation. Roblox, il logo Roblox e Powering Imagination sono tra i nostri marchi registrati e non registrati negli Stati Uniti. e altri paesi.