Attachment System Trouble

Hello I’m Currently making an Attachment System that would take any attachment the player chooses from a list of already existing attachments and add it on a weapon so far I’ve gone really far with it but ran into a few problems, it would only work for the first Weapon, then it would error when it’s not the First Weapon on the Table(tell me if I’m not clear enough) OH and i forgot the attachments won’t really look “Attached” whenever i move the part around since I’m using world position and couldn’t use CFrame

attachementNumber = 1
local AttachmentSpace = {}

local function ButtonPress(parts)
	local NewWeaponGui = script.Attachments:Clone()
	NewWeaponGui.Parent = mainFrame.Attachments.ScrollingFrame.AttachmentList
	NewWeaponGui.Text = parts.Name
	NewWeaponGui.Name = parts.Name
	NewWeaponGui.MouseButton1Down:connect(function()
		if AttachmentAmount ~= MaxAttachments then
			AttachmentAmount = AttachmentAmount + 1
			local NewWeapon = AttachmentsTable[table.find(AttachmentsTable, parts)]:Clone()
			local FindWeapon = Cusomtization.ClonePartHere:FindFirstChild(WeaponsTable[number].Name)
			local AttachName = ""
			NewWeapon.Parent = FindWeapon:WaitForChild("Attachments")
			for i, partsAttachment in ipairs(FindWeapon:GetChildren()) do
				if partsAttachment:IsA("Attachment") then
					attachementNumber = attachementNumber + 1
					if attachementNumber == #AttachmentSpace + 1 then
						attachementNumber = 1
					end

					if partsAttachment:FindFirstChild("Middle").Value == true and not partsAttachment:GetAttribute("Occupied") == true then
						AttachName = FindWeapon:FindFirstChild(partsAttachment.Name)
						table.find(AttachmentSpace, i)
						print(partsAttachment)
					end
				end
			end			
			print(AttachName)
			NewWeapon:SetPrimaryPartCFrame(CFrame.new(AttachName.WorldPosition) * CFrame.Angles(0,math.rad(90),0))
			
			local Weld = Instance.new("WeldConstraint")
			Weld.Parent = NewWeapon
			Weld.Part0 = NewWeapon.PrimaryPart
			Weld.Part1 = FindWeapon
			AttachName:SetAttribute("Occupied", true)
		end
	end)
end

As it is giving nil, because maybe it is returning a string, it gives the error, change it to this

attachementNumber = 1
local AttachmentSpace = {}

local function ButtonPress(parts)
	local NewWeaponGui = script.Attachments:Clone()
	NewWeaponGui.Parent = mainFrame.Attachments.ScrollingFrame.AttachmentList
	NewWeaponGui.Text = parts.Name
	NewWeaponGui.Name = parts.Name
	NewWeaponGui.MouseButton1Down:connect(function()
		if AttachmentAmount ~= MaxAttachments then
			AttachmentAmount = AttachmentAmount + 1
			local NewWeapon = AttachmentsTable[table.find(AttachmentsTable, parts)]:Clone()
			local FindWeapon = Cusomtization.ClonePartHere:FindFirstChild(WeaponsTable[number].Name)
			local AttachName = ""
			NewWeapon.Parent = FindWeapon:WaitForChild("Attachments")
			for i, partsAttachment in ipairs(FindWeapon:GetChildren()) do
				if partsAttachment:IsA("Attachment") then
					attachementNumber = attachementNumber + 1
					if attachementNumber == #AttachmentSpace + 1 then
						attachementNumber = 1
					end

					if partsAttachment:FindFirstChild("Middle").Value == true and not partsAttachment:GetAttribute("Occupied") == true then
						AttachName = FindWeapon:FindFirstChild(partsAttachment.Name)
						table.find(AttachmentSpace, i)
						print(partsAttachment)
					end
				end
			end			
			print(AttachName)
			if typeof(AttachName) ~= "Instance" or not AttachName:IsA("Attachment") then				return				end
			NewWeapon:SetPrimaryPartCFrame(CFrame.new(AttachName.WorldPosition) * CFrame.Angles(0,math.rad(90),0))

			local Weld = Instance.new("WeldConstraint")
			Weld.Parent = NewWeapon
			Weld.Part0 = NewWeapon.PrimaryPart
			Weld.Part1 = FindWeapon
			AttachName:SetAttribute("Occupied", true)
		end
	end)
end

typeof will verify that it is an instance and IsA will verify that it is an Attachment.

1 Like

yeah the string prints out nothing unless it’s the grenade uhhh