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

IncrementAsync

This function increments the value of a key by the provided amount (both must be integers).

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

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

int
1

Amount to increment the current value by.

{}

(Optional) A table of Player/UserId|UserIds to associate with the key.

nil

(Optional) DataStoreIncrementOptions instance that combines multiple additional parameters as custom metadata and allows for future extensibility.

Returns

Return Type Summary

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


Code Samples


Increment Data Store Key/Value

local DataStoreService = game:GetService("DataStoreService")

local experienceStore = DataStoreService:GetDataStore("PlayerExperience")

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

local success, newExperience, keyInfo = pcall(function()
	return experienceStore:IncrementAsync("User_1234", 10, {1234}, incrementOptions)
end)
if success then
	print(newExperience)
	print(keyInfo.Version)
	print(keyInfo.CreatedTime)
	print(keyInfo.UpdatedTime)
	print(keyInfo:GetUserIds())
	print(keyInfo:GetMetadata())
end