API:Class/GlobalDataStore/IncrementAsync

IncrementAsync

YieldFunction of ExplorerImageIndex69.pngGlobalDataStore
Variant IncrementAsync (
    string key,
    int delta = 1
)
Parameters:
  1. key
  2. delta
    • Type: int
    • Defaults to: 1

Returns: Variant
Description: Increments the value for a particular key and returns the incremented value. Only works on values that are integers. 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

In the following example we will use a DataStore to increase the value of a key, so as to reflect how many times a player has visited your game. Due to IncrementAsync returning the incremented value, we can award the player based on how many times they have visited.

local DataStore = game:GetService("DataStoreService"):GetDataStore("PlayerVisits")
 
game.Players.PlayerAdded:connect(function(Player)
    local PlayerKey = "user_" .. Player.userId
    local Visits = DataStore:IncrementAsync(PlayerKey, 1)
    if Visits == 10 then
        --// Reward them for visiting ten times. 
    end
end)