Change values deep in a dictonary

Hello! I want to update a list deeply. Not sure how to explain it so here is an example:
I have this list:

Players = {
  ["123456"] = {
    ["Value1"] = 123,
    ["Value2"] = true
  },.
  ["14532"] = {
    ["Value1"] = 321,
    ["Value2"] = false
  }
}

and I would like to change the value in Players > 123456 > Value2 to false. How would I do this? I have this function that doesnt work:

module.update = function(newData)
    --Reference the table above
	local read = module.read()
	
	local function check(tempList)
		for valname,oldVal in tempList do
			if not newData[valname] then continue end
			if not tempList[valname] then tempList[valname] = newData[valname] continue end
			if typeof(oldVal) == "table" then
				tempList[valname] = check(tempList[valname])
			else
				tempList[valname] = newData[valname]
			end
		end
		return tempList
	end
	check(read)
	return read
end

And to update it I would like to do this with the update function:

module.update({
  ["123456"] = {
    -- Doesn't effect any values except these under 123456
    ["Value1"] = 1856
    ["Value2"] = false
  }
})

Thanks! If I left out anything or need more info, just send a reply!

1 Like

Do you want something more concise than?

Players["123456"]["Value2"] = false

I want to be able to call this function like so:

module.update({
  ["123456"] = {
    ["Value1"] = 1856
    ["Value2"] = false
  }
})

And it only changing the value1 and value2 for this player.

I don’t have time to test this right this second but maybe something like this?

function update(changes, previous)
	previous = previous or Players

	for k, v in pairs(changes) do
		if previous[k] ~= nil then
			if type(v) == "table" then
				update(changes[k], previous[k])
			else
				previous[k] = v
			end
		end
	end
end

So I am doing this in my main script:

gamedata.update({
	["Players"] = {
		[tostring(player.UserId)] = {
			["Class"] = class
		},
		["ARAWRARAWRWAr"] = {
			["Class"] = "hey.",
			["Real"] = true
		}
	}
})
gamedata.update({
	["Players"] = {
		[tostring(player.UserId)] = {
			["Class"] = "errrrrrrrr"
		}
	}
})

and in my GameData module script:

-- ALL UNDER UPDATE FUNCTION
local read = module.read()
	
local function update(changes, previous)
	for k, v in pairs(changes) do
		if previous[k] ~= nil then
			if type(v) == "table" then
				update(changes[k], previous[k])
			else
				previous[k] = v
			end
		end
	end
end
update(newData, read)
print(`changed value:`, read)

and the console:

changed value:  ▼  {
  ["Gamemode"] = 1,
  ["Players"] = {}
} 
changed value:  ▼  {
  ["Gamemode"] = 1,
  ["Players"] = {}
}