Correctly adding a new value to a DataStore when another value already exists

Hi. I’m simply wondering how to correctly put in a new value into a DataStore without messing up other old values. I tried the method I thought worked, but it erased my other value. I am simply using the recommended DataStore code from documentation as shown:

local function setupPlayerData(player)
	local playerUserId = "Player_" .. player.UserId
	local data
	local success, err = pcall(function()
		data = playerData:GetAsync(playerUserId)
	if success then
		if data then
			-- Data exists for this player
			sessionData[playerUserId] = data	
			-- Data store is working, but no current data for this player
			sessionData[playerUserId] = {Points=0, Wins=0}
		warn("Cannot access data store for player!")

If the points already exists for a player, how do I add wins in with a value of zero without messing with the points value? Since data is available for the player (because points exist for them), how do I add new wins data with a value of zero?

I just want to make sure I do it correctly.

You can just directly change the Wins index, like this:

sessionData[playerUserId].Wins = 10

Which won’t touch the Points

1 Like

Thanks. I just did this to check if the “Wins” value doesn’t exist yet:

  sessionData[playerUserId] = data
  if sessionData[playerUserId]["Wins"] == nil then
    sessionData[playerUserId] = {Points=sessionData[playerUserId]["Points"], Wins=0}

I think I was overcomplicating it. My bad. :joy: