I have a table that I need to make a copy of, and I need to make modifications to the data in the second table without any of these modifications applying to the original table. But I haven’t been able to figure out how to do this.
local t1 = {[Vector3.new(1, 2, 3)] = {1, "2", true, nil, {5, 6, 7}}, [Vector3.new(4, 5, 6)] = {7, "8", true, nil, {9, 10, 11}}}
print(t1[Vector3.new(4, 5, 6)][1]) -- This prints 7
local t2 = table.clone(t1)
t2[Vector3.new(4, 5, 6)][1] = 100
print(t2[Vector3.new(4, 5, 6)][1]) -- This prints 100
print(t1[Vector3.new(4, 5, 6)][1]) -- This one also prints 100, but it's actually only supposed to print 7, because the modification was made to t2, and not t1
In the code, I am setting a value in the table t2
to equal to the value of 100
. And on line 5
when I print this value from the table t2
, it indeed does print 100
, but when we print the value from the same exact index, but on the other table (table t1
) on line 6
, it also prints 100
. What is happening? Why does this happen? This doesn’t make sense? I expect it to print 7
when printing it from table t1
, but instead it prints 100
.
I apologize for the strange keys in these dictionaries, but for the tables that I am having this issue in, I need to use Vector3.new(x, y, z)
keys for the table that I am having issues with.