PcoWSkbVqDnWTu_dm2ix
Collapse Sidebar

GetVersionAsync

This function retrieves the specified key version as well as a DataStoreKeyInfo instance. A version identifier can be found through DataStore/ListVersionsAsync or alternatively be the identifier returned by GlobalDataStore/SetAsync.

See Also

  • Articles/Data store|Data Stores, an in-depth guide on data structure, management, error handling, etc.

Parameters

Name Type Default Description

key

Key name for which the version info is requested. If DataStoreOptions/AllScopes was set to true when accessing the data store through DataStoreService/GetDataStore, this key name must be prepended with the original scope as in “scope/key”.

Version number of the key for which the version info is requested

Returns

Return Type Summary

The value of the key at the specified version and a DataStoreKeyInfo instance that includes the version number, date and time the version was created, and functions to retrieve Player/UserId|UserIds and metadata.


Code Samples


Restore Closest Key Version

local DataStoreService = game:GetService("DataStoreService")

-- Enable experimental features (beta period only)
local options = Instance.new("DataStoreOptions")
options:SetExperimentalFeatures({["v2"] = true})

local experienceStore = DataStoreService:GetDataStore("PlayerExperience", "", options)

local DATA_STORE_KEY = "User_1234"

local maxDate = DateTime.fromUniversalTime(year=2020, month=10, day=09, hour=01, min=42)

-- Get the version closest to the given time
local listSuccess, pages = pcall(function()
	return experienceStore:ListVersionsAsync(DATA_STORE_KEY, Enum.SortDirection.Descending, nil, maxDate.UnixTimestampMillis)
end)
if listSuccess then
	local items = pages:GetCurrentPage()
	if table.getn(items) > 0 then
		-- Read the closest version
		local closestEntry = items[1]
		local success, value, info = pcall(function()
			return experienceStore:GetVersionAsync(DATA_STORE_KEY, closestEntry.Version)
		end)
		-- Restore current value by overwriting with the closest version
		if success then
			local setOptions = Instance.new("DataStoreSetOptions")
			setOptions:SetMetadata(info:GetMetadata())
			experienceStore:SetAsync(DATA_STORE_KEY, value, nil, setOptions)
		end
	else
		-- No entries found
	end
end