How do i "update" a table

So basically I want to turn one table to another sort of like this

local Template = {
     ValueA = 0,
     ValueB = 0,
}
local OutdatedTable = {
     ValueA = 100
}

func update(template,tabletoupdate)
--magic here
return upDatedTable
end

--updatedtable result:

{
     ValueA = 100,
     ValueB = 0
}

So what I want to do is “copy over” the template to my outdated table without erasing existing value

Im not asking for exact code, just any methods because I’m stumped

Ive tried some stuff on the devforum but they all weren’t what I was looking for.

You could do it, :face_with_monocle: looks good test it. Hope you tell me if it’s working !

local Template = {
     ValueA = 0,
     ValueB = 0,
}
local OutdatedTable = {
     ValueA = 100
}

function update(template, outdatedTable)
    local updatedTable = {}
    for k, v in pairs(template) do
        if outdatedTable[k] then
            updatedTable[k] = outdatedTable[k]
        else
            updatedTable[k] = v
        end
    end
    return updatedTable
end

local updatedTable = update(Template, OutdatedTable)
print(updatedTable)
3 Likes

Thanks! It works perfectly. I didn’t know it was that simple lol.
Thanks again!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.