UpdateAsync - pass an argument to the transform function

Hello.
When I do that, it works.

local dataStore = game:GetService("DataStoreService")
local dataPlayers = dataStore:GetDataStore("DataPlayers")

local function IncGold(data, keyInfo)
	data.gold += 50
	return data
end
dataPlayers:UpdateAsync(tostring(player.UserId), IncGold)

But I would like to change the value to increase.
Like this :

local function IncGold(goldAdd , data, keyInfo)
	data.gold += goldAdd
	return data
end
dataPlayers:UpdateAsync(tostring(player.UserId), IncGold(goldAdd))

I know this code is not working because it returns goldAdd, nil, nil.
But why can’t an Update function, like UpdateAsync, pass a simple argument, the value to update?

Is there a way to do what I want with UpdateAsync or do I have to do a GetAsync, change the data then SetAsync?
Thanks for your help

Hello, method UpdateAsync requires a function as parameter, not result of the function, what you can do is to box the logic into another function and return it

local function IncGold(goldAdd)
    return function(data, keyInfo)
        data.gold += goldAdd
        return data
    end
end

dataPlayers:UpdateAsync(tostring(player.UserId), IncGold(goldAdd))

This code is not tested, however I’m pretty sure the result will look similar to this.

2 Likes

That works.
It would have been hard to find that on my own.
Thanks a lot Cloudy71.