DataStoreService

顯示已棄用項目

*此內容是使用 AI(Beta 測試版)翻譯,可能含有錯誤。若要以英文檢視此頁面,請按一下這裡

無法建立
服務
未複製

數據儲存服務 暴露方法來獲得 GlobalDataStoreOrderedDataStore 對象。數據存儲只能通過遊戲服務器訪問,因此您只能在 DataStoreServiceScript 中使用 ModuleScriptScript 遊戲服務器使用的數據存儲。

參見數據儲存以獲得關於數據結構、管理、錯誤處理等方面的詳細指南

範例程式碼

This code sample prints the request budget for all data store request types.

DataStore Budget

local DataStoreService = game:GetService("DataStoreService")
for _, enumItem in pairs(Enum.DataStoreRequestType:GetEnumItems()) do
print(enumItem.Name, DataStoreService:GetRequestBudgetForRequestType(enumItem))
end

概要

屬性

  • 未複製
    本機使用者安全性
    平行讀取

    設置數據存儲功能是否自動重試。數據儲存服務沒有遵守此屬性,因為技術原因自動重試已停用。因此,您必須自行實現操作重試系統。

方法

屬性

AutomaticRetry

未複製
本機使用者安全性
平行讀取

設置數據存儲功能是否自動重試。

數據儲存服務沒有遵守此屬性,因為技術原因自動重試已停用。因此,您必須自行實現操作重試系統。未來可能會再次啟用自動重試。

方法

GetDataStore

這個功能會創建一個 DataStore 實例,使用提供的名稱和範圍。使用相同名稱/範圍的後續呼叫此方法將返回相同的對物件。

使用 scope 參數將限制操作到該範圍,通過自動將範圍添加到所有在數據存商店 商家上執行的操作的鑰匙。此功能也接受可選的 DataStoreOptions 實例,包括啟用 AllScopes 的選項。請參閱數據儲存以獲得有關範圍的詳情。

參數

name: string

數據存商店 商家的名稱。

預設值:""
scope: string

(可選) 一個指定範圍的字串。

預設值:"global"
options: Instance

(可選) 一個 DataStoreOptions 實例來啟用實驗功能和 v2 API 功能。

預設值:"nil"

返回

一個具有提供的名稱和可選範圍的 DataStore 實例。

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.

Get GlobalDataStore Instance

local DataStoreService = game:GetService("DataStoreService")
local GlobalDataStore = DataStoreService:GetGlobalDataStore()
print(GlobalDataStore.Name)

GetOrderedDataStore

此方法返回 OrderedDataStore , 與 GetDataStore() 使用 GlobalDataStores 類似。使用相同名稱/範圍的後續呼叫此方法將返回相同的對物件。

參數

name: string
預設值:""
scope: string
預設值:"global"

返回

範例程式碼

This code sample demonstrates usage of an OrderedDataStore and pages.

OrderedDataStore Basics

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 來執行的數量資料儲存請求。超過此預算的任何請求都會受到限制。建議使用此功能監控和調整數據儲存請求的頻率。

參數

預設值:""

返回

範例程式碼

Print Request Budget

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 參數僅找到那些名稱以提供的前缀開始的數據儲存。

只有包含至少一個對象的資料存儲會透過此功能列出。

參數

prefix: string

(可選) 前缀用於枚舉開始於給定前缀的數據儲存。

預設值:""
pageSize: number

(可選) 每頁要返回的項目數量。如果沒有提供值,引擎會將預設值 0 傳送到資料儲存網路服務,該服務會轉而預設每頁 32 項。

預設值:0
cursor: string

(可選) 滑鼠繼續循環。

預設值:""

返回

DataStoreListingPages 包含 DataStoreInfo 個包含詳細資訊,例如名稱、創建時間和上次更新時間的實例。

活動