API:Class/GlobalDataStore/SetAsync

SetAsync

YieldFunction of ExplorerImageIndex69.pngGlobalDataStore
void SetAsync (
    string key,
    Variant value
)
Parameters:
  1. key
  2. value

Returns: void
Description: Sets the value of the key. This overwrites any existing data stored in the key.

If the previous value of the key is important, use UpdateAsync. Using GetAsync to retrieve a value and then setting the key with SetAsync is dangerous because GetAsync sometimes returns cached data, and other game servers may have modified the key. Mind that there are limits that apply to this.

If this function throws an error, the error message will describe the problem. A full list of DataStore errors can be seen here.

Example

The following example is how you would generally use SetAsync. However, if more than one server will likely be writing to this key, it is recommended that you use UpdateAsync instead.

local DataStore = game:GetService("DataStoreService"):GetDataStore("PlayerVisits")
 
game.Players.PlayerAdded:connect(function(Player)
    local PlayerKey = "user_" .. Player.userId
    DataStore:SetAsync(PlayerKey, 0)
end)

See Also