請參閱數據儲存。
概要
方法
取得指定的鑰匙版本。
取得指定時間點的鑰匙版本。
- 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
將鑰匙的值增加所提供的數量(兩者都必須是整數)。
移除指定的鑰匙,同時保留可存取的版本。
設置資料儲存的值為指定的鍵。
使用指定的回呼函數的新值來更新鑰鍵的值。
屬性
方法
GetVersionAsync
此功能會擷取指定的鑰匙版本以及 DataStoreKeyInfo 個實個體、實例。版本標識可以通過 DataStore:ListVersionsAsync() 找到,或者可以是由 GlobalDataStore:SetAsync() 返回的標識。
參數
版本資訊要求的關鍵名稱。如果 DataStoreOptions.AllScopes 在通過 DataStoreService:GetDataStore() 存取資料儲存時設為真實,此鍵名必須與原始範圍一樣在 "scope/key" 中加以前缀。
版本號為要求版本資訊的鑰匙的版本號。
返回
指定版本的鑰匙值和包含版本號、日期和時間的 DataStoreKeyInfo 實例,以及用於恢復 UserIds 和元數據的功能。
GetVersionAtTimeAsync
此功能會擷取在指定時間的鑰匙版本以及 DataStoreKeyInfo 個實個體、實例。
參數
版本資訊要求的關鍵名稱。如果 DataStoreOptions.AllScopes 在通過 DataStoreService:GetDataStore() 存取資料儲存時設為真實,此鍵名必須與原始範圍一樣在 "scope/key" 中加以前缀。
以秒為單位的 Unix 時戳,該要求的版本為當前。必須大於零。未來不得超過十分鐘。
返回
指定時間的鑰匙值和包含版本號、日期和時間的 DataStoreKeyInfo 實例,以及用於取得版本 UserIds 和元數據的功能。nil在要求時間沒有可用版本時。
範例程式碼
The following code sample retrieves data store key versions using timestamps.
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() 存取資料儲存時設為真實,鑰匙將用所有範圍作為前缀返回。
參數
(可選) 用於找到鍵的前缀。
(可選) 每頁要返回的項目數量。如果沒有提供值,引擎會將預設值 0 傳送到資料儲存網路服務,該服務會轉而預設每頁 50 項。
(可選) 滑鼠繼續循環。
(可選) 排除刪除的鑰匙不被返回。
啟用時,列鑰將檢查最多 512 個鑰匙。如果所有已選擇的鍵都被刪除,則會返回一個空白列表,包括鼠標以繼續迭代。
返回
一個 DataStoreKeyPages 枚列鑰匙為 DataStoreKey 實例的案例。
ListVersionsAsync
此功能列出指定的鑰匙版本,以上升或下降順序指定由 Enum.SortDirection 參數所指定的順序。它可選擇過濾返回的版本以最小和最大時間戳。
參數
列出版本的關鍵名稱。如果 DataStoreOptions.AllScopes 在通過 DataStoreService:GetDataStore() 存取資料儲存時設為真實,此鍵名必須與原始範圍一樣在 "scope/key" 中加以前缀。
(可選) Enum指定上升或下降排序順序。
(可選) Unix 時戳在毫秒後,版本應被列出。
(可選) Unix 時戳在毫秒內,版本應列出到哪裡。
(可選) 每頁要返回的項目數量。如果沒有提供值,引擎會將預設值 0 傳送到資料儲存網路服務,該服務會轉而預設每頁 1024 項。
返回
一個 DataStoreVersionPages 枚列所有版本的鑰匙為 DataStoreObjectVersionInfo 實例的案例。
範例程式碼
The following code sample retrieves all versions after a specified starting time, sorted in ascending order.
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() 不同,此功能不會創建新的「墓碑」版本,且移除的值無法稍後恢復。
參數
版本要移除的鑰匙名稱。如果 DataStoreOptions.AllScopes 在通過 DataStoreService:GetDataStore() 存取資料儲存時設為真實,此鍵名必須與原始範圍一樣在 "scope/key" 中加以前缀。
要移除的鑰匙版本號。