PcoWSkbVqDnWTu_dm2ix
The Developer Hub is now deprecated and information on this page may no longer be accurate. To see our new and improved documentation, please click here. You can read more about the future of documentation here.
Collapse Sidebar

UpdateAsync

This function retrieves the value and metadata of a key from the data store and updates it with a new value determined by the callback function specified through the second parameter.

If the update succeeds, a new version of the value will be created and prior versions will remain accessible through DataStore/ListVersionsAsync and DataStore/GetVersionAsync.

OrderedDataStore does not support versioning, so calling this function on an OrderedDataStore key will overwrite the current value and make previous versions inaccessible.

Set vs. Update

GlobalDataStore/SetAsync is best for a quick update of a specific key, and it only counts against the write limit. However, it may cause data inconsistency if two servers attempt to set the same key at the same time.

GlobalDataStore/UpdateAsync is safer for handling multi-server attempts because it reads the current key value (from whatever server last updated it) before making any changes. However, it's somewhat slower because it reads before it writes, and it also counts against both the read and write limit.

Callback Function

The callback function accepts two arguments:

  • Current value of the key prior to the update.
  • DataStoreKeyInfo instance that contains the latest version information (this argument can be ignored if metadata is not being used).

In turn, the callback function returns up to three values:

  • The new value to set for the key.
  • An array of Player/UserId|UserIds to associate with the key. DataStoreKeyInfo/GetUserIds should be returned unless the existing IDs are being changed; otherwise all existing IDs will be cleared.
  • A Lua table containing metadata to associate with the key. DataStoreKeyInfo/GetMetadata should be returned unless the existing metadata is being changed; otherwise all existing metadata will be cleared.

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 value should be updated. 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”.

Transform function that takes the current value and DataStoreKeyInfo as parameters and returns the new value along with optional Player/UserId|UserIds and metadata.

Returns

Return Type Summary

The updated value of the entry in the data store with the given key 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


Update Data Store Value

local DataStoreService = game:GetService("DataStoreService")

local nicknameStore = DataStoreService:GetDataStore("Nicknames")

local function makeNameUpper(currentName, keyInfo)
	local nameUpper = string.upper(currentName)
	local userIDs = keyInfo:GetUserIds()
	local metadata = keyInfo:GetMetadata()
	return nameUpper, userIDs, metadata
end

local success, updatedName, keyInfo = pcall(function()
	return nicknameStore:UpdateAsync("User_1234", makeNameUpper)
end)
if success then
	print(updatedName)
	print(keyInfo.CreatedTime)
	print(keyInfo:GetMetadata())
end