PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

IncrementAsync

Increments the value for a particular key and returns the incremented value. Only works on values that are integers. Note that you can use GlobalDataStore/OnUpdate|OnUpdate() to execute a function every time the database updates the key’s value, such as after calling this function.

If this function throws an error, the Articles/Datastore Errors|error message will describe the problem. Note that there are also Articles/Datastore Errors|limits that apply to this function.

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

Parameters

Name Type Default Description

key

The key identifying the entry being retrieved from the data store

int
1

The increment amount

Returns

Return Type Summary

The value of the entry in the data store with the given key


Code Samples


Increment Data Store

This example uses a data store to increase the value of a key, reflecting how many times a player has visited your game. Because GlobalDataStore/IncrementAsync|IncrementAsync() returns the incremented value, you can reward the player based on how many times they’ve visited.

local visitsDataStore = game:GetService("DataStoreService"):GetDataStore("PlayerVisits")

game.Players.PlayerAdded:Connect(function(player)
	local playerKey = "Player_" .. player.UserId

	local visits
	local success, err = pcall(function()
		visits = visitsDataStore:IncrementAsync(playerKey, 1)
	end)

	if success and visits == 10 then
		-- Reward player for visiting ten times
	end
end)