數據儲存服務 暴露方法來獲得 GlobalDataStore 和 OrderedDataStore 對象。數據存儲只能通過遊戲服務器訪問,因此您只能在 DataStoreService 或 Script 中使用 ModuleScript 或 Script 遊戲服務器使用的數據存儲。
參見數據儲存以獲得關於數據結構、管理、錯誤處理等方面的詳細指南
範例程式碼
This code sample prints the request budget for all data store request types.
local DataStoreService = game:GetService("DataStoreService")
for _, enumItem in pairs(Enum.DataStoreRequestType:GetEnumItems()) do
print(enumItem.Name, DataStoreService:GetRequestBudgetForRequestType(enumItem))
end
概要
屬性
設置數據存儲功能是否自動重試。數據儲存服務沒有遵守此屬性,因為技術原因自動重試已停用。因此,您必須自行實現操作重試系統。
方法
創建一個 DataStore 實例,使用提供的名稱和範圍。
返回預設資料商店 商家存。
獲得 OrderedDataStore 一個名稱和可選範圍。
返回指定的請求類輸入可以提出的請求數量。
返回一個 DataStoreListingPages 對象來枚評所有體驗數據存儲。
屬性
方法
GetDataStore
這個功能會創建一個 DataStore 實例,使用提供的名稱和範圍。使用相同名稱/範圍的後續呼叫此方法將返回相同的對物件。
使用 scope 參數將限制操作到該範圍,通過自動將範圍添加到所有在數據存商店 商家上執行的操作的鑰匙。此功能也接受可選的 DataStoreOptions 實例,包括啟用 AllScopes 的選項。請參閱數據儲存以獲得有關範圍的詳情。
參數
數據存商店 商家的名稱。
(可選) 一個指定範圍的字串。
(可選) 一個 DataStoreOptions 實例來啟用實驗功能和 v2 API 功能。
返回
GetGlobalDataStore
此功能返回預設 GlobalDataStore 。如果您想要使用特定的 命名的 數據存儲,您應該使用 GetDataStore() 函數。
請注意,由此函數返回的 DataStore 總是使用範圍 u 。請參閱 資料儲存 以了解範圍的詳情。
返回
範例程式碼
The following example retrieves a default data store instance which behaves like a regular Instance. Since a GlobalDataStore is an Instance, functions such as GlobalDataStore:GetChildren() will execute without error.
local DataStoreService = game:GetService("DataStoreService")
local GlobalDataStore = DataStoreService:GetGlobalDataStore()
print(GlobalDataStore.Name)
GetOrderedDataStore
此方法返回 OrderedDataStore , 與 GetDataStore() 使用 GlobalDataStores 類似。使用相同名稱/範圍的後續呼叫此方法將返回相同的對物件。
參數
返回
範例程式碼
This code sample demonstrates usage of an OrderedDataStore and pages.
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()
-- The data in 'topTen' is stored with the index being the index on the page
-- For each item, 'data.key' is the key in the OrderedDataStore and 'data.value' is the value
for rank, data in ipairs(topTen) do
local name = data.key
local points = data.value
print(name .. " is ranked #" .. rank .. " with " .. points .. "points")
end
-- Potentially load the next page...
--pages:AdvanceToNextPageAsync()
end
-- Create some data
pointsStore:SetAsync("Alex", 55)
pointsStore:SetAsync("Charley", 32)
pointsStore:SetAsync("Sydney", 68)
-- Display the top ten players
printTopTenPlayers()
GetRequestBudgetForRequestType
此功能返回當前位置可以根據指定的 Enum.DataStoreRequestType 來執行的數量資料儲存請求。超過此預算的任何請求都會受到限制。建議使用此功能監控和調整數據儲存請求的頻率。
參數
返回
範例程式碼
local DataStoreService = game:GetService("DataStoreService")
local globalStore = DataStoreService:GetGlobalDataStore()
local function printBudget()
local budget = DataStoreService:GetRequestBudgetForRequestType(Enum.DataStoreRequestType.SetIncrementAsync)
print("Current set/increment budget:", 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
end
ListDataStoresAsync
返回一個 DataStoreListingPages 對象來枚評所有體驗數據存儲。它接受可選擇的 prefix 參數僅找到那些名稱以提供的前缀開始的數據儲存。
只有包含至少一個對象的資料存儲會透過此功能列出。
參數
(可選) 前缀用於枚舉開始於給定前缀的數據儲存。
(可選) 每頁要返回的項目數量。如果沒有提供值,引擎會將預設值 0 傳送到資料儲存網路服務,該服務會轉而預設每頁 32 項。
(可選) 滑鼠繼續循環。
返回
DataStoreListingPages 包含 DataStoreInfo 個包含詳細資訊,例如名稱、創建時間和上次更新時間的實例。