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!
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.
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?