So I'm trying to make a shop, and when a player buys an item the following code runs. Everything prints just fine, and even at the end it prints true. However, the async is not updated and still returns false. Why?
button = script.Parent price = 738 player = button.Parent.Parent.Parent.Parent.Parent.Parent.Parent data = game:GetService("DataStoreService"):GetDataStore("MWTestData1") function inventory(old) if not old.arm then print(player.Name.." hasn't bought the arm") old.arm = true game.ServerStorage.PlayerStats[player.Name].Stats.Points.Value = game.ServerStorage.PlayerStats[player.Name].Stats.Points.Value - price print(player.Name.." purchased "..button.Parent.Name.."!") pointskey = player.userId.."Points" data:IncrementAsync(pointskey, -price) points = game.ServerStorage.PlayerStats[player.Name].Stats.Points points.Value = data:GetAsync(player.userId.."Points") print(player.Name.."'s points: "..points.Value) else print("Arm already purchased") end print(old..": "..old.arm) return old end function buyItem() print(player.Name.." is attempting to buy "..button.Parent.Name) if game.ServerStorage.PlayerStats[player.Name].Stats.Points.Value >= price then print(player.Name.." has enough points") data:UpdateAsync(player.userId.."Inventory", inventory) else print(player.Name.." doesn't have enough points") end end button.MouseButton1Click:connect(buyItem)
Thanks! --MightyWanderer
UpdateAsync's second argument is a function that returns the new value.
Here's the example the Roblox wiki uses to explain it:
function timesTwo(input) return input*2 end ds = game:GetService("DataStoreService"):GetGlobalDataStore() ds:UpdateAsync("mykey", timesTwo) -- This will change the value of "mykey" to the output value of the -- timesTwo(value) function, with '''''value''''' as the current value for "mykey".
The upside of UpdateAsync is that there is no risk of an out-of-date value being used.