Does this seem right for updateAsync()

I have this and if a new player leaves the new game would the updateasync set the new value to Coins.Value since their is no prior value?

local leaderstats = Player.leaderstats 
local Coins = leaderstats.Coins

local Success, Response = pcall(function()
    CoinDataStore:UpdateAsync(key, function(oldValue)
        local newValue = oldValue or Coins.Value
        return newValue
    end)
end)

If the player leaves, I recommend using SetAsync to save their data as UpdateAsync is would consider the old value.

1 Like

Your code will only save new data if the player had no previous data.

1 Like

How can I save new data?

Should I make it so the new value is equal to Coins.Value

1 Like

If the saved data is equal to nil, then save using :SetAsync. If it is not, safe using :UpdateAsync. At least that’s what I understand from this thread.

Yes, old data isn’t really used in many situations that I can think off.

@ItzMeZeus_IGotHacked Just because UpdateAsync returns the old value doesn’t mean that is the only feature it has. Please read the other features of UpdateAsync from this topic: Stop using SetAsync() to save player data - #21 by Affenity

@n_cy Yes, it will. That oldValue or Coins.Value states that If oldValue exists, don’t use Coins.Value. Else, use Coins.Value

Can I have like a More detailed version of it? I don’t quite understand.

The link I sent is the most detailed. It explains everything to why SetAsync is bad for updating data and the other features UpdateAsync has other than the oldValue.