Help with IncrementAsync

Hello, I’m currently using the old datastore and I want to switch to datastore 2. First thing I’m gonna do is to increment the value of an item when it gets bought in the shop but it doesn’t seem to work. I’m not sure if IncremenAsync also works for bool values.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DataStoreService = game:GetService('DataStoreService')
local PlayerItemsDS = DataStoreService:GetDataStore('PlayerItems')
local RunService = game:GetService("RunService")
local BuyItem = game.ReplicatedStorage.BuyItem

local function BuyThisItem(player, price, item)
	if player.leaderstats.Points.Value >= price then
		player.leaderstats.Points.Value  = player.leaderstats.Points.Value - price
		player.Items[item].Value = true
		PlayerItemsDS:IncrementAsync(item, true)
		print("Datastore Update: "..item)
	end
end

BuyItem.OnServerEvent:Connect(BuyThisItem)

It doesn’t give me an error and it prints out. I’m not sure if I’m missing something. Thank you!

In the IncrementAsync function, you specify the item, but not the quantity. Where you put true, should be a number.

So bool values won’t work here?

No it wont. IncrementAsync is a function to increase a number. You would need to use SetAsync or UpdateAsync.

1 Like

IncrementAsync is typically used in conjunction with OrderedDataStores as it’s used for storing number values only, when you call “IncrementAsync()” with a given key and a specified number value, the existing value stored inside the DataStore for that key (if one exists) is incremented, as the name of the function suggests, by the value specified.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DataStoreService = game:GetService('DataStoreService')
local PlayerItemsDS = DataStoreService:GetDataStore('PlayerItems')
local RunService = game:GetService("RunService")
local BuyItem = game.ReplicatedStorage.BuyItem

local function BuyThisItem(player, price, item)
	if player.leaderstats.Points.Value >= price then
		player.leaderstats.Points.Value -= price
		player.Items[item].Value = true
		PlayerItemsDS:SetAsync(player.UserId.."_"..item, true)
		print("Datastore Update: "..item)
	end
end

BuyItem.OnServerEvent:Connect(BuyThisItem)

I suggest storing the player’s ID somewhere inside the key, that way you’ll know which data/values pertain to which users. In the script I provided, I’ve concatenated the ID of the user which purchased an item with the name of the item itself, I’ve also changed “IncrementAsync()” to “UpdateAsync()” to allow for Boolean values to be stored. If you plan on having multiple items/purchases stored in this way I suggest using a dictionary of items with each corresponding to a key & value pair, the key being the item it self & the value being a Boolean value representing whether or not the item has or has not been purchased.

How would I increment a value to a key in the case of another existing value?

success, err = pcall(function()
	playerDataStore:UpdateAsync(playerKey, playerData[playerKey])
	playerDataStore:IncrementAsync(playerKey, playerData[playerKey].SessionLengthValue)
end)

The following code saves a table named playerData to playerKey. With this, I am attempting to increment a player’s JoinTimeValue (equal to time, a value under playerData) to their total playtime. Do I need to differentiate the playerData and JointimeValue if using IncrementAsync() under the same key?

Appreciated,
vamp

Datastore2 is outdated and shouldn’t be used. This has been stated by the creator of Datastore2 himself (as seen in this link:)

I recommend using Suphi’s Datastore Module (my preference) or ProfileService (both linked below).

I’m sorry, but how does this relate to my question? I’m not using any plugins for data storage.

Because this post was about datastore2 so I thought your post was about it as well, my mistake.

1 Like

All good, thank you for your help!