Table.insert adding to 2 tables

Hello,

I am having a weird issue where I am trying to add something to one table yet for some reason, it’s adding to the table that I want it to add too but it’s also adding to another table? The attachments table is for telling the script what attachments should be on the gun and the appliedattachments is telling the script that those attachments have been applied. For some reason, when I do table.insert(attachments,attachment) it’s also adding to appliedattachments. I have no idea why and when I check the prints for both tables on the ApplyAttachment function right under where it adds it, it shows that it adds both of them inside both of those tables because of that one line. Not sure if that makes sense, I can give more info if needed.

Script:

local camera = Library.Workspace.CurrentCamera
local gunModel:Model = nil
local attachments = nil
local appliedAttachments = nil
local loadout = nil
local connection = nil

function Update()
	if not loadout then return end
	local save = Library.Save.Get() if not save then return end
	local gun = loadout[1]
	local gunDir = Library.Directory.Guns.GetGun(gun)
	
	if not gunModel then
		gunModel = gunDir.gunModel:Clone()
		gunModel.Parent = loadoutRoom
		gunModel:PivotTo(gunPos.CFrame)
		dragTemplate:Clone().Parent = gunModel.PrimaryPart
	end
	if not attachments then
		attachments = loadout[2] or {}
		appliedAttachments = attachments
		connection = Library.Attachments:new(gun,gunModel,attachments)
	else
		for i, attachment in ipairs(attachments) do
			if not table.find(appliedAttachments,attachment) then
				local result = connection:apply(attachment)
				if result ~= "ERROR 408" then
					table.insert(appliedAttachments,attachment)
				else
					table.remove(attachments,table.find(attachments,attachment))
				end
			end
		end
		
		for i, attachment in ipairs(appliedAttachments) do
			if not table.find(attachments,attachment) then
				connection:remove(attachment)
				table.remove(appliedAttachments,table.find(appliedAttachments,attachment))
			end
		end
	end
	
end
function module.ApplyAttachment(attachment)
	if not attachment or not loadout or not attachments or not appliedAttachments or not gunModel then return end
	
	table.insert(attachments,attachment)
	Update()
end
function module.RemoveAttachment(attachment)
	if not attachment or not loadout or not attachments or not appliedAttachments or not gunModel then return end
	local _attachment = table.find(attachments,attachment) if not _attachment then return end
	
	table.remove(attachments,_attachment)
	Update()
end
2 Likes

As a result of “pass by reference” for tables, if I remember this correctly, there is only one table and appliedAttachments refer to attachments as a reference, not as a clone of its value. Therefore we experience this effect that one table is affecting another table since it is not pass by value.

To solve this, just create a separate table containing exactly the same thing.

2 Likes

Ah, I think I understand what you’re saying. Let me clone the table instead and see if that fixes my problem.

1 Like

Works perfectly, thank you. I changed appliedAttachments = attachments to appliedAttachments = Library.Functions.CloneTable(attachments)

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.