I might just be having a brainfart or something to I’ve been stuck on this for the past couple of hours. Here is an example:
local t = {
[1] = 'A', [2] = 'B', [3] = 'C'
}
local t2 = t
t2[1] = 'X'; t2[2] = 'Y'; t2[3] = 'Z';
print(t, t2)
I’m wanting to clone Table1, defining the clone as Table2 and edit the data inside of Table2. However, the data I edit in Table2 also edits the data in Table1. I’ve tried looping through Table1 and adding all the items into Table2 instead of just defining Table2 as Table1 but still the same problem.
So the output should be:
Table1: A, B C
Table2: X, Y, Z
This is an example, of course I’d create a new table however the tables I am actually using have 50+ lines in them which I don’t want to copy and paste over and over since I’m only needing to edit 1 or 2 lines in each one.
Are the indices numerical or other? If so, use deepCopy.
local function deepCopy(original)
local copy = {}
for k, v in pairs(original) do
if type(v) == "table" then
v = deepCopy(v)
end
copy[k] = v
end
return copy
end
local original = {
[1] = "A"
[2] = "B"
}
local clone = shallowCopy(original)
local function shallowCopy(original)
local copy = {}
for key, value in pairs(original) do
copy[key] = value
end
return copy
end
local DataBase = {
["BoolValue"] = {
Image = "rbxassetid://4525769967",
ImageColor3 = Color3.fromRGB(255,255,255),
ImageRectOffset = Vector2.new(64,0),
ImageRectSize = Vector2.new(16,16),
ImageTransparency = 0,
Properties = {
['Data'] = { lOrd = 1,
[1] = {"ClassName", true},
[2] = {"Name"},
[3] = {"Parent"},
[4] = {"Value"},
},
['Behavior'] = { lOrd = 2,
[1] = {"Archivable"},
},
}
},
}
--//
local function DupeTable(t)
local nt = {}
for i,v in pairs(t) do
nt[i] = v
end
return nt
end
for _,v in pairs({"Color3Value", "CustomEvent","CustomEventReceiver","DoubleConstrainedValue","FloorWire","IntConstrainedValue",
"IntValue","NumberValue","ObjectValue","StringValue","Vector3Value","BrickColorValue", "CFrameValue", "RayValue"}) do
local newT = DupeTable(DataBase['BoolValue'])
DataBase[v] = newT
end
DataBase['CFrameValue'].Properties['Data'][4] = nil
DataBase['RayValue'].Properties['Data'][4] = nil
print(
DataBase['CFrameValue'].Properties['Data'][4], -- should be nil
DataBase['BoolValue'].Properties['Data'][4] -- should be a table/true
)
BoolValue in the database never gets edited so I dont know why it gets changed to nil, it’s only changed when you change CFrameValue or RayValue to nil but they arent linked to BoolValue as it’s a duplicate tables as seen from the DupeTable function
local function DupeTable(original)
local copy = {}
for k, v in pairs(original) do
if type(v) == "table" then
v = DupeTable(v)
end
copy[k] = v
end
return copy
end
When you use shallowCopy, the key is a and the value is the reference to the tbl.a table. That means that if we try to edit tbl2.a, then we’ll really be editing tbl.a since tbl2.a is just a reference to tbl.a.
Here’s a brief explanation that I wrote a while ago.
Admittedly I could probably word it better, but hopefully you can get something out of it.
Edit: Here’s a more recent one that might be more understandable.