DataStoreService

显示已弃用

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

无法创建
服务
未复制

数据存储服务 暴露方法获取 GlobalDataStoreOrderedDataStore 对象。数据存储只能通过游戏服务器访问,因此您只能在 或使用 由 使用的 中使用 。

请参阅数据存储获取有关数据结构、管理、错误处理等方面的详尽指南

代码示例

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 实例,其中包含提供详细信息,例如名称、创建时间和上次更新时间的实例。

活动