数据存储服务 暴露方法获取 GlobalDataStore 和 OrderedDataStore 对象。数据存储只能通过游戏服务器访问,因此您只能在 或使用 由 使用的 中使用 。
请参阅数据存储获取有关数据结构、管理、错误处理等方面的详尽指南
代码示例
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 的选项。请参阅版本管理、列表和缓存获取有关范围的详细信息。
参数
返回
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 实例,其中包含提供详细信息,例如名称、创建时间和上次更新时间的实例。