Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

UpdateAsync isn't working - why?

Asked by 10 years ago

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

1 answer

Log in to vote
0
Answered by
Merely 2122 Moderation Voter Community Moderator
10 years ago

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.

0
But that's what I'm doing. If you read in the buyItem function, I have "data:UpdateAsync(player.userId.."Inventory", inventory)" --"inventory" in the second part refers to the inventory funtion up above, which works just like it says on the wiki. MightyWanderer 30 — 10y
Ad

Answer this question