Overwriting tables

Overwriting tables has always been on my mind for some time. I wanted to know if overwriting tables in this manner would be feasible and wouldn’t lead to potential issues.

local A = {Test = true, Class = "Sword"}
local B = {Test = false, Class = "Dagger", Skills = {Slash = true}}
A = B

Now I’ve tested this out and it does actually work. However, I can’t help but feel that somehow some things would be missing or incorrect.

Sure, I could manually do this:

A.Test = false
A.Class = "Dagger"
A.Skills = {Slash = true}

But this isn’t convenient. I’m using Profile Service as a means to store data in my game but it saves based on tables, and not folders, values, etc. This means I have to serialize my player folder and convert it into something that could actually be saved. The only issue is that this would be a very tedious process of making sure everything is properly aligned, especially if some values get deleted, added, or moved around.

This would work, however, that makes A point to the table B points to, so something like

local t = { }
local t2 = t

table.insert(t2, "a")
print(t[1])

will print "a", since t2 points to the same table t points to. I don’t know if you want a copy instead.

1 Like