เรียนรู้
Engine Class
MemoryStoreHashMap
ไม่สามารถสร้าง
ไม่ซ้ำ

*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่


สรุป
วิธีการ
GetAsync(key: string):Variant
SetAsync(key: string,value: Variant,expiration: number):boolean
UpdateAsync(key: string,transformFunction: function,expiration: number):Variant
สมาชิกที่ได้รับทอด

เอกสารอ้างอิงเกี่ยวกับ API
วิธีการ
GetAsync
ผลตอบแทน
ความสามารถ: DataStore
MemoryStoreHashMap:GetAsync(key:string):Variant
พารามิเตอร์
key:string
ส่งค่ากลับ
Variant
ตัวอย่างโค้ด
การดึงข้อมูลจาก 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, _ = pcall(function()
return hashMap:SetAsync(key, value, expiration)
end)
if setSuccess then
print("การตั้งค่าประสบความสำเร็จ!")
end
local item
local getSuccess, getError = pcall(function()
item = hashMap:GetAsync(key)
end)
if getSuccess then
print(item)
else
warn(getError)
end

ListItemsAsync
ผลตอบแทน
ความสามารถ: DataStore
MemoryStoreHashMap:ListItemsAsync(count:number):MemoryStoreHashMapPages
พารามิเตอร์
count:number
ส่งค่ากลับ
ตัวอย่างโค้ด
การแสดงรายการรายการในแผนที่แฮช MemoryStore
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("กำลังตั้งค่าข้อมูล HashMap...")
local createdItems = {}
for index = 1, numItems do
local key = tostring(index) -- คีย์ HashMap ต้องเป็นสตริง
local value = `{key}_test_value`
local success, result = pcall(hashMap.SetAsync, hashMap, key, value, EXPIRATION)
if success then
createdItems[key] = value
else
warn(`เกิดข้อผิดพลาดในการตั้งค่าคีย์ {key}: {result}`)
end
end
print("เสร็จสิ้นการตั้งค่าข้อมูล HashMap.")
return createdItems
end
local function getItemsFromAllPages(pages: MemoryStoreHashMapPages): { [string]: any }
-- เพื่อวัตถุประสงค์ในการบันทึกข้อมูล เราจะติดตามหมายเลขหน้าที่เราอยู่
local currentPageNumber = 1
local retrievedItems = {}
while not pages.IsFinished do
print(`กำลังดึงรายการในหน้า {currentPageNumber}...`)
local items = pages:GetCurrentPage()
for _, entry in pairs(items) do
print(` {entry.key}: {entry.value}`)
retrievedItems[entry.key] = entry.value
end
-- เลื่อนหน้าหากยังมีหน้าที่ต้องอ่าน
if not pages.IsFinished then
pages:AdvanceToNextPageAsync()
currentPageNumber += 1
end
end
print("เสร็จสิ้นการอ่านหน้าทั้งหมด")
return retrievedItems
end
local function compareAllItems(retrievedItems: { [string]: any }, expectedItems: { [string]: any }): number
print("กำลังเปรียบเทียบรายการที่ดึงมาสำหรับรายการที่คาดหวัง...")
local numMatchingItems = 0
for key, expectedValue in pairs(expectedItems) do
if retrievedItems[key] == expectedValue then
numMatchingItems += 1
else
warn(`ค่าที่ดึงมาสำหรับคีย์ {key} ไม่ตรงกัน: คาดหวัง {expectedValue}, ดึงมา {retrievedItems[key]}`)
end
end
print("การเปรียบเทียบเสร็จสิ้น!")
return numMatchingItems
end
-- คีย์ที่เพิ่มลงในแผนที่แฮชจะถูกเพิ่มไปยังตาราง expectedItems นี้ด้วย
-- ต่อมา รายการแผนที่แฮชที่ดึงมาจะถูกเปรียบเทียบกับตารางของรายการที่คาดหวังนี้
local expectedItems = populateHashMap(testHashMap, NUM_TEST_ITEMS)
-- การดึงหน้าสามารถเกิดข้อผิดพลาดได้ ในกรณีนี้เราจะปล่อยให้มันเกิดข้อผิดพลาดและหยุดการทำงานของโปรแกรม,
-- แต่คุณอาจต้อง pcall และจัดการกับมันแตกต่างออกไป
print(`กำลังดึงหน้าของ HashMap ด้วย ListItemsAsync...`)
local pages = testHashMap:ListItemsAsync(NUM_TEST_ITEMS)
local retrievedItems = getItemsFromAllPages(pages)
local numMatchingItems = compareAllItems(retrievedItems, expectedItems)
-- หากไม่มีข้อผิดพลาดในการตั้งค่าหรือดึงรายการ รายการทั้งหมดควรตรงกัน.
print(`โปรแกรมเสร็จสิ้น. {numMatchingItems}/{NUM_TEST_ITEMS} รายการที่ดึงมาตรงกับค่าที่คาดหวัง.`)

RemoveAsync
ผลตอบแทน
ความสามารถ: DataStore
MemoryStoreHashMap:RemoveAsync(key:string):()
พารามิเตอร์
key:string
ส่งค่ากลับ
()
ตัวอย่างโค้ด
การลบข้อมูลจากแผนที่แฮชของ 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("การลบสำเร็จ!")
else
warn(removeError)
end

SetAsync
ผลตอบแทน
ความสามารถ: DataStore
MemoryStoreHashMap:SetAsync(
key:string, value:Variant, expiration:number
พารามิเตอร์
key:string
value:Variant
expiration:number
ส่งค่ากลับ
ตัวอย่างโค้ด
การเพิ่มข้อมูลลงในแผนที่แฮชของ 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 setSuccess then
print("การตั้งค่าประสบความสำเร็จ!")
else
warn(setError)
end

UpdateAsync
ผลตอบแทน
ความสามารถ: DataStore
MemoryStoreHashMap:UpdateAsync(
key:string, transformFunction:function, expiration:number
):Variant
พารามิเตอร์
key:string
transformFunction:function
expiration:number
ส่งค่ากลับ
Variant
ตัวอย่างโค้ด
การอัปเดตแฮชแมพของหน่วยความจำ
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
-- ตรวจสอบว่าเราไม่เกินจำนวนทรัพยากรสูงสุด
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, โลโก้ Roblox และ Powering Imagination เป็นส่วนหนึ่งของเครื่องหมายการค้าที่จดทะเบียน และไม่ได้จดทะเบียนของเราในสหรัฐฯ และประเทศอื่นๆ