Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I use IncrementAsync Correctly?

I have a datastore where I indexed the player's data using their player.UserId.

The players datatable looks like this:

player.UserId = {
				PlayerStats = {Money = 10, etc..}
				OtherTables..
			{

I want to increment Money by 10 every 10 seconds and I was wondering if IncrementAsync would be a good option.

If it is, how would I use it to do this?

If not, should I just make a local value and increment that and save it at the end of the game into the datastore?

Thank you!

1 comment

0

I've looked at the roblox page for IncrementAsync and it's not very clear on how to use it.

1 answer

✓ Accepted answer

Question

How do I use IncrementAsync Correctly?

Answer

For sake of reference: https://developer.roblox.com/en-us/api-reference/function/GlobalDataStore/IncrementAsync

You call the method with a datastore key, and a value to increment that datastore key's value by. IncrementAsync(key, value) is equivalent to SetAsync(key, GetAsync(key) + value) in terms of what it does.

Example

local dataStoreService = game:GetService("DataStoreServiec")
local myDataStore = dataStoreService:GetDataStore("MyDataStore")

--// assuming there is already a datastore key "epic levels"
myDataStore:IncrementAsync("epic levels", 100)

This code is functionally equivalent to this:

local dataStoreService = game:GetService("DataStoreServiec")
local myDataStore = dataStoreService:GetDataStore("MyDataStore")

--// assuming there is already a datastore key "epic levels"
local epicLevels = myDataStore:GetAsync("epic levels")
epicLevels:SetAsync("epic levels", epicLevels + 1)

This discussion is preserved as it was. New answers, comments, and votes are closed.