请参见 数据存储。
概要
方法
检索指定的密钥版本。
检索在给定时间的当前密钥版本。
- ListKeysAsync(prefix : string,pageSize : number,cursor : string,excludeDeleted : boolean):DataStoreKeyPages
返回一个 DataStoreKeyPages 对象,用于遍历数据存储的密钥。
- ListVersionsAsync(key : string,sortDirection : Enum.SortDirection,minDate : number,maxDate : number,pageSize : number):DataStoreVersionPages
枚举密钥的所有版本。
永久删除指定版本的密钥。
方法
返回指定数据存储中键的值和一个 DataStoreKeyInfo 实例。
- IncrementAsync(key : string,delta : number,userIds : Array,options : DataStoreIncrementOptions):Variant
将键的值按提供的数量递增(两者必须为整数)。
删除指定的键,同时保留可访问的版本。
为给定键设置数据存储的值。
使用指定回调函数的值更新键的值。
属性
方法
GetVersionAtTimeAsync
参数
返回
代码示例
按时间检索 DataStore 版本
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("DataStore")
local key = "key-123"
function setData(data)
local success, result = pcall(function()
dataStore:SetAsync(key, data)
end)
if not success then
warn(result)
end
end
function getVersionAtTime(timestamp)
local success, result, keyInfo = pcall(function()
return dataStore:GetVersionAtTimeAsync(key, timestamp.UnixTimestampMillis)
end)
if success then
if result == nil then
print("在此时间找不到版本")
else
print(result, keyInfo.Version)
end
else
warn(result)
end
end
-- 之前在 2024/12/02 6:00 UTC 运行
setData("version 1")
-- 之前在 2024/12/02 9:00 UTC 运行
setData("version 2")
-- 打印 "在此时间找不到版本"
local time1 = DateTime.fromUniversalTime(2024, 12, 02, 05, 00)
getVersionAtTime(time1)
-- 打印 "version 1 <version>"
local time2 = DateTime.fromUniversalTime(2024, 12, 02, 07, 00)
getVersionAtTime(time2)
-- 打印 "version 2 <version>"
local time3 = DateTime.fromUniversalTime(2024, 12, 02, 10, 00)
getVersionAtTime(time3)
ListKeysAsync
参数
返回
ListVersionsAsync
参数
默认值:"Ascending"
默认值:0
默认值:0
默认值:0
返回
代码示例
使用日期过滤器检索数据存储版本
local DataStoreService = game:GetService("DataStoreService")
local experienceStore = DataStoreService:GetDataStore("PlayerExperience")
local time = DateTime.fromUniversalTime(2020, 10, 09, 01, 42)
local listSuccess, pages = pcall(function()
return experienceStore:ListVersionsAsync("User_1234", nil, time.UnixTimestampMillis)
end)
if listSuccess then
local items = pages:GetCurrentPage()
for key, info in pairs(items) do
print("键:", key, "; 版本:", info.Version, "; 创建时间:", info.CreatedTime, "; 已删除:", info.IsDeleted)
end
end
属性继承自GlobalDataStore