UpdateAsync With Multiple Values

I was wondering if anyone could help me with using UpdateAsync. I completely understand SetAsync, but recently I was told to switch over to this instead. On the Roblox Wiki I saw some code:

local pointsDataStore = game:GetService("DataStoreService"):GetDataStore("Points")
 
game.Players.PlayerAdded:Connect(function(player)
    local playerKey = "Player_" .. player.UserId
    -- Give 50 points to players each time they visit
    local success, err = pcall(function()
        pointsDataStore:UpdateAsync(playerKey, function(oldValue)
            local newValue = oldValue or 0
            newValue = newValue + 50
            return newValue
        end)
    end)
end)

What is oldValue since it is never defined before the function and can I put multiple values into this?

I tried to find online resources for this, but no luck. If anyone would like to help or give me resources that would help I would greatly appreciate it.

ALSO I am specifically trying to save a dictionary, but with UpdateAsync and possibly explore JSONencoding.

EDIT:
Here is my code:

local DataStoreService = game:GetService("DataStoreService")

local PlayerStats = DataStoreService:GetDataStore("PlayerStats1")

local DefaultStats = {
	
	["CurrentStats"] = {
		
		["CityLevel"] = 0,
		["CityPopulation"] = 0,
		["CityVisits"] = 0,
		["CoinsPerSecond"] = 0,
		["Coins"] = 0,
		["Cash"] = 0,
		["AchievementsCompleted"] = 0,
		["HasAirport"] = false,
		["HasForeverDoubleEarnings"] = false,
		
	},
	
	["HighestStats"] = {
	
		["HighestCityLevel"] = 0,
		["HighestCityPopulation"] = 0,
		["HighestCoins"] = 0,
		["HighestCash"] = 0,
		["HighestAchievementsCompleted"] = 0	
		
	}
	
	
}


game:GetService("Players").PlayerAdded:Connect(function(Player)
	
	local playerKey = tostring("key-"..Player.UserId.."-devtechgames")
	
	--what do i do ._.
	
end)

Oldvalue is the previous stored value. Let’s say if a user has no data it’ll be nil and therefore it will chose 0 (due to oldvalue or 0) after this in the wiki function it adds another 50 to the new value and then stores the new value making it safer to set data. If adjusting a table I’d suggest doing oldvalue or standardTableTemplate as example. Setting the newtable as return type assumed the data retrieved and adjusted got stored on the server for eventual storing in the datastorage.

At the playerAdded you simply retrieve the data by using a GetAsync witin a Pcall (to catch errors).

Resources on this are fairly limited as far as I’m aware in which i mean basically the roblox developers wiki. But i do hope i provided enough information.

(This was written on a phone so apologies for the lack of structure and code. I tried to explain it as precise as possible)

2 Likes

After reading your response, I now understand but I will still play around with it to really get familiar.
Thank you!