DataStore

顯示已棄用項目

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

無法建立
未複製

請參閱數據儲存

概要

方法

方法 繼承自 GlobalDataStore

屬性

方法

GetVersionAsync

暫停

此功能會擷取指定的鑰匙版本以及 DataStoreKeyInfo 個實個體、實例。版本標識可以通過 DataStore:ListVersionsAsync() 找到,或者可以是由 GlobalDataStore:SetAsync() 返回的標識。

參數

key: string

版本資訊要求的關鍵名稱。如果 DataStoreOptions.AllScopes 在通過 DataStoreService:GetDataStore() 存取資料儲存時設為真實,此鍵名必須與原始範圍一樣在 "scope/key" 中加以前缀。

預設值:""
version: string

版本號為要求版本資訊的鑰匙的版本號。

預設值:""

返回

指定版本的鑰匙值和包含版本號、日期和時間的 DataStoreKeyInfo 實例,以及用於恢復 UserIds 和元數據的功能。

GetVersionAtTimeAsync

暫停

此功能會擷取在指定時間的鑰匙版本以及 DataStoreKeyInfo 個實個體、實例。

參數

key: string

版本資訊要求的關鍵名稱。如果 DataStoreOptions.AllScopes 在通過 DataStoreService:GetDataStore() 存取資料儲存時設為真實,此鍵名必須與原始範圍一樣在 "scope/key" 中加以前缀。

預設值:""
timestamp: number

以秒為單位的 Unix 時戳,該要求的版本為當前。必須大於零。未來不得超過十分鐘。

預設值:""

返回

指定時間的鑰匙值和包含版本號、日期和時間的 DataStoreKeyInfo 實例,以及用於取得版本 UserIds 和元數據的功能。nil在要求時間沒有可用版本時。

範例程式碼

The following code sample retrieves data store key versions using timestamps.

Retrieving DataStore Versions by Time

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("No version found at time")
else
print(result, keyInfo.Version)
end
else
warn(result)
end
end
-- Previously ran at 2024/12/02 6:00 UTC
setData("version 1")
-- Previously ran at 2024/12/02 9:00 UTC
setData("version 2")
-- Prints "No version found at time"
local time1 = DateTime.fromUniversalTime(2024, 12, 02, 05, 00)
getVersionAtTime(time1)
-- Prints "version 1 <version>"
local time2 = DateTime.fromUniversalTime(2024, 12, 02, 07, 00)
getVersionAtTime(time2)
-- Prints "version 2 <version>"
local time3 = DateTime.fromUniversalTime(2024, 12, 02, 10, 00)
getVersionAtTime(time3)

ListKeysAsync

暫停

此功能返回一個 DataStoreKeyPages 對象,用於通過數據存儲商店 商家的鑰匙枚舉。它接受可選擇的 prefix 參數僅找到名稱以提供的前缀開始的鑰匙。

如果 DataStoreOptions.AllScopes 在通過 DataStoreService:GetDataStore() 存取資料儲存時設為真實,鑰匙將用所有範圍作為前缀返回。

參數

prefix: string

(可選) 用於找到鍵的前缀。

預設值:""
pageSize: number

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

預設值:0
cursor: string

(可選) 滑鼠繼續循環。

預設值:""
excludeDeleted: boolean

(可選) 排除刪除的鑰匙不被返回。

啟用時,列鑰將檢查最多 512 個鑰匙。如果所有已選擇的鍵都被刪除,則會返回一個空白列表,包括鼠標以繼續迭代。

預設值:false

返回

一個 DataStoreKeyPages 枚列鑰匙為 DataStoreKey 實例的案例。

ListVersionsAsync

暫停

此功能列出指定的鑰匙版本,以上升或下降順序指定由 Enum.SortDirection 參數所指定的順序。它可選擇過濾返回的版本以最小和最大時間戳。

參數

key: string

列出版本的關鍵名稱。如果 DataStoreOptions.AllScopes 在通過 DataStoreService:GetDataStore() 存取資料儲存時設為真實,此鍵名必須與原始範圍一樣在 "scope/key" 中加以前缀。

預設值:""
sortDirection: Enum.SortDirection

(可選) Enum指定上升或下降排序順序。

預設值:"Ascending"
minDate: number

(可選) Unix 時戳在毫秒後,版本應被列出。

預設值:0
maxDate: number

(可選) Unix 時戳在毫秒內,版本應列出到哪裡。

預設值:0
pageSize: number

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

預設值:0

返回

一個 DataStoreVersionPages 枚列所有版本的鑰匙為 DataStoreObjectVersionInfo 實例的案例。

範例程式碼

The following code sample retrieves all versions after a specified starting time, sorted in ascending order.

Retrieving DataStore Versions With A Date Filter

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:", key, "; Version:", info.Version, "; Created:", info.CreatedTime, "; Deleted:", info.IsDeleted)
end
end

RemoveVersionAsync

()
暫停

此功能永久刪除指定版本的鍵匙。版本標識可以通過 DataStore:ListVersionsAsync() 找到。

GlobalDataStore:RemoveAsync() 不同,此功能不會創建新的「墓碑」版本,且移除的值無法稍後恢復。

參數

key: string

版本要移除的鑰匙名稱。如果 DataStoreOptions.AllScopes 在通過 DataStoreService:GetDataStore() 存取資料儲存時設為真實,此鍵名必須與原始範圍一樣在 "scope/key" 中加以前缀。

預設值:""
version: string

要移除的鑰匙版本號。

預設值:""

返回

()

活動