I’ve first encountered this problem on vanilla Lua 5.3 but I also have the same problem on Roblox Lua (5.1). I want to make a function that merges the table.
function merge(t1, t2)
for k, v in next, t2 do
if (type(v) == "table") and (type(t1[k]) == "table") then
t1[k] = merge(t1[k], t2[k]);
else
t1[k] = v;
end;
end;
return t1;
end;
local a = { test = 'test1' };
local b = { test = 'test2' };
local c = a;
print(a.test);
print(merge(a, b).test);
print(a.test);
print(c.test);
It should’ve outputted
test1
test2
test1
test1
But the code somehow output the result differently:
This is also a problem in Roblox lua:
The problem is that the table ‘a’ is somehow set to the merged function when the merge function is run. I don’t want that to happen, how do I make it so both the merge function runs and the table ‘a’ isn’t changing.