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

SetAsync

This function sets the latest value, Player/UserId|UserIds, and metadata for the given key.

Values in data stores are versioned, meaning GlobalDataStore/SetAsync will create a new version every time it is called. Prior versions can be accessed through DataStore/ListVersionsAsync/DataStore/GetVersionAsync for up to 30 days at which point they are permanently deleted.

OrderedDataStore does not support versioning, so calling this method 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.

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 set. 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”.

The value that the data store key will be set to.

{}

Table of Player/UserId|UserIds, highly recommended to assist with GDPR tracking/removal.

nil

(Optional) DataStoreSetOptions instance that allows for metadata specification on the key.

Returns

Return Type Summary

The version identifier of the newly created version. It can be used to retrieve key info using DataStore/GetVersionAsync|GetVersionAsync() or to remove it using DataStore/RemoveVersionAsync|RemoveVersionAsync().


Code Samples


Set Data Store Key/Value

The following code sample demonstrates how to set a key’s value and include an optional table of Player/UserId|UserIds along with a DataStoreSetOptions instance that defines metadata for the key.

local DataStoreService = game:GetService("DataStoreService")

local experienceStore = DataStoreService:GetDataStore("PlayerExperience")

local setOptions = Instance.new("DataStoreSetOptions")
setOptions:SetMetadata({["ExperienceElement"] = "Fire"})

local success, errorMessage = pcall(function()
	experienceStore:SetAsync("User_1234", 50, {1234}, setOptions)
end)
if not success then
	print(errorMessage)
end