Modifying Table2 modifies Table1?

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

1 Like

I am a

at tables but I see that you are editing a table. It’s logical to edit the 1st table if you are overwriting it from the 2nd table.

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.

I think I know what you want to do.
For cloning tables, you can try to do something like this:

local original = {1, 3, 5, 7, 9}
local clone = {unpack(original)}
 
for i = 1, #clone do
	print(clone[i])
end

You should use a copy function to copy the table without editing the first one.

function tableCopy(tbl)
    local tbl2 = {}
    for _, v in ipairs(tbl) do
        table.insert(tbl2, v)
    end
    return tbl2
end

You can’t unpack a dictionary when the index’s aren’t numerical, in my case they are strings

that’s what I did and I’m still have the same issue

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
1 Like

A slightly edited code of @R0bl0x10501050

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

His indices are not numerical, that won’t work. Nevermind

You need to define shallowCopy first.

Here is the code I’m trying to duplicate

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

Change DupeTable to

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
3 Likes

Yeah that worked, so I have to duplicate the tables inside it as well?

The function automatically duplicates the internal tables when it reaches one, hence the name “deepCopy”.

1 Like

Yeah I can see that, I’m just confused why you’d have to copy the data inside the table as well

Given this table:

local tbl = {
    a = {
        'b'
    }
}

tbl.a refers the the table inside tbl, named a.

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.