How do you change the value of something inside a Data Store?

  1. What I want to achieve?
    I have made a data store that saves the in-game currency of the player. And once the player purchases something from the shop it should update the currency value inside the data store.

  2. What is the issue?
    I tried using UpdateAsync() to change the currency’s value but it’s value stays the exact same, no changes in value at all.

Here’s the script:

	local cost = RS:WaitForChild("Weapons"):WaitForChild("Gun"):GetAttribute("Cost") --Cost of the item
	if player.leaderstats.Cash.Value >= tonumber(cost) then --If the player's cash >= cost
		local result = dataStore:UpdateAsync(player.UserId, function(OldcashValue)
			local newCashValue = OldcashValue[1]
			newCashValue = newCashValue - cost --this should subtract the cash's value by the cost if the player has enough
			return newCashValue -- returns the value

Always read the documentation. Roblox has an entire site dedicated to documenting every event/function of their services

In your case of trying to update a value in the datastore, I’d recommend the :UpdateAsync() function on GlobalDatastores.

Everything you need regarding the datastore service can be found in this documentation.