เรียนรู้
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
-- คีย์ที่เพิ่มใน hashmap จะถูกเพิ่มไปยังตาราง expectedItems นี้ด้วย
-- ต่อมา รายการ hashmap ที่ดึงมาจะถูกเปรียบเทียบกับตารางที่คาดหวังนี้
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} รายการที่ดึงมา match กับค่าที่คาดหวัง.`)

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
ส่งค่ากลับ
ตัวอย่างโค้ด
การเพิ่มข้อมูลลงในแผนที่ Hash ของ 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 เป็นส่วนหนึ่งของเครื่องหมายการค้าที่จดทะเบียน และไม่ได้จดทะเบียนของเราในสหรัฐฯ และประเทศอื่นๆ