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