Engine Class
DataStoreService
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
สรุป
วิธีการ
GetDataStore(name: string,scope: string,options: Instance):DataStore |
GetOrderedDataStore(name: string,scope: string):OrderedDataStore |
GetRequestBudgetForRequestType(requestType: Enum.DataStoreRequestType):number |
ListDataStoresAsync(prefix: string,pageSize: number,cursor: string):DataStoreListingPages |
SetRateLimitForRequestType(requestType: Enum.DataStoreRequestType,baseLimit: number,perPlayerLimit: number):() |
ตัวอย่างโค้ด
งบประมาณ DataStore
local DataStoreService = game:GetService("DataStoreService")
for _, enumItem in pairs(Enum.DataStoreRequestType:GetEnumItems()) do
print(enumItem.Name, DataStoreService:GetRequestBudgetForRequestType(enumItem))
endเอกสารอ้างอิงเกี่ยวกับ API
วิธีการ
GetDataStore
GetGlobalDataStore
GetOrderedDataStore
ส่งค่ากลับ
ตัวอย่างโค้ด
พื้นฐาน OrderedDataStore
local DataStoreService = game:GetService("DataStoreService")
local pointsStore = DataStoreService:GetOrderedDataStore("Points")
local function printTopTenPlayers()
local isAscending = false
local pageSize = 10
local pages = pointsStore:GetSortedAsync(isAscending, pageSize)
local topTen = pages:GetCurrentPage()
-- ข้อมูลใน 'topTen' ถูกจัดเก็บด้วยดัชนีซึ่งเป็นดัชนีในหน้า
-- สำหรับแต่ละรายการ, 'data.key' คือคีย์ใน OrderedDataStore และ 'data.value' คือค่า
for rank, data in ipairs(topTen) do
local name = data.key
local points = data.value
print(name .. " อยู่ในอันดับที่ #" .. rank .. " มีคะแนน " .. points .. "คะแนน")
end
-- อาจโหลดหน้าถัดไป...
--pages:AdvanceToNextPageAsync()
end
-- สร้างข้อมูลบางอย่าง
pointsStore:SetAsync("Alex", 55)
pointsStore:SetAsync("Charley", 32)
pointsStore:SetAsync("Sydney", 68)
-- แสดง 10 ผู้เล่นชั้นนำ
printTopTenPlayers()GetRequestBudgetForRequestType
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
งบประมาณการพิมพ์คำขอ
local DataStoreService = game:GetService("DataStoreService")
local globalStore = DataStoreService:GetGlobalDataStore()
local function printBudget()
local budget = DataStoreService:GetRequestBudgetForRequestType(Enum.DataStoreRequestType.SetIncrementAsync)
print("งบประมาณการตั้งค่า/เพิ่มปัจจุบัน:", budget)
end
for i = 1, 5 do
local key = "key" .. i
local success, err = pcall(function()
globalStore:SetAsync(key, true)
end)
if success then
printBudget()
else
print(err)
end
endListDataStoresAsync
DataStoreService:ListDataStoresAsync(
ส่งค่ากลับ
SetRateLimitForRequestType
DataStoreService:SetRateLimitForRequestType(
):()
พารามิเตอร์
ส่งค่ากลับ
()
ตัวอย่างโค้ด
ตั้งค่าขีดจำกัดอัตรา
local MigrateScript = require(script.Parent.MigrateScript)
local DataStoreService = game:GetService("DataStoreService")
DataStoreService:SetRateLimitForRequestType(Enum.DataStoreRequestType.StandardRead, 1000, 0)
DataStoreService:SetRateLimitForRequestType(Enum.DataStoreRequestType.StandardWrite, 2000, 0) -- เขียนสองครั้ง ดังนั้นต้องการขีดจำกัดการเขียนเป็นสองเท่า
DataStoreService:SetRateLimitForRequestType(Enum.DataStoreRequestType.StandardList, 100, 0)
DataStoreService:SetRateLimitForRequestType(Enum.DataStoreRequestType.StandardRemove, 1000, 0)
local sourceStore = DataStoreService:GetDataStore("ds_to_migrate")
local destinationStore1 = DataStoreService:GetDataStore("ds_destination_1")
local destinationStore2 = DataStoreService:GetDataStore("ds_destination_2")
-- ตัวช่วยในการรอขีดจำกัดงบประมาณ
local function waitForBudget(requestType)
while (DataStoreService:GetRequestBudgetForRequestType(requestType) <= 0) do
task.wait(1)
end
end
waitForBudget(Enum.DataStoreRequestType.StandardList)
local success, pages = pcall(function()
return sourceStore:ListKeysAsync()
end)
if success then
while true do
local items = pages:GetCurrentPage()
for _, item in ipairs(items) do
-- 1. ดึงข้อมูลจากต้นทาง
waitForBudget(Enum.DataStoreRequestType.StandardRead)
local getSuccess, value = pcall(function()
return sourceStore:GetAsync(item.KeyName)
end)
if getSuccess and value ~= nil then
-- 2. ย้ายข้อมูลไปยังจุดหมายปลายทาง
waitForBudget(Enum.DataStoreRequestType.StandardWrite)
local setSuccess, err = pcall(function()
destinationStore1:SetAsync(item.KeyName, MigrateScript.Migrate1(value))
end)
if setSuccess then
print("ย้ายคีย์ไปยังจุดหมายปลายทาง 1 อย่างสำเร็จ: " .. item.KeyName)
else
warn("ไม่สามารถย้ายคีย์ไปยังจุดหมายปลายทาง 1: " .. item.KeyName .. " - " .. tostring(err))
end
waitForBudget(Enum.DataStoreRequestType.StandardWrite)
local setSuccess, err = pcall(function()
destinationStore2:SetAsync(item.KeyName, MigrateScript.Migrate2(value))
end)
if setSuccess then
print("ย้ายคีย์ไปยังจุดหมายปลายทาง 2 อย่างสำเร็จ: " .. item.KeyName)
else
warn("ไม่สามารถย้ายคีย์ไปยังจุดหมายปลายทาง 2: " .. item.KeyName .. " - " .. tostring(err))
end
-- 3. ลบข้อมูลจากต้นทาง
waitForBudget(Enum.DataStoreRequestType.StandardRemove)
local deleteSuccess, err = pcall(function()
sourceStore:RemoveAsync(item.KeyName)
end)
if deleteSuccess then
print("ลบคีย์จากต้นทางอย่างสำเร็จ: " .. item.KeyName)
else
warn("ไม่สามารถลบคีย์จากต้นทาง: " .. item.KeyName .. " - " .. tostring(err))
end
else
warn("ไม่สามารถดึงคีย์จากต้นทาง: " .. item.KeyName)
end
end
if pages.IsFinished then break end
-- 3. ไปยังหน้าถัดไป
waitForBudget(Enum.DataStoreRequestType.StandardList)
local nextSuccess = pcall(function()
pages:AdvanceToNextPageAsync()
end)
if not nextSuccess then
warn("ไม่สามารถไปยังหน้าถัดไปได้.")
break
end
end
print("กระบวนการย้ายข้อมูลเสร็จสิ้น.")
else
warn("ไม่สามารถแสดงคีย์ได้.")
end