Script cannot detect some attachments after using FindFirstChild

I really dont know which accesories you wanna remove or not, I just did read that you mentioned Hair, Face, Hat. And I saw a list you made too in ur replies… So I just copied that. You gotta customize the list for ur needs. And I added a little script that checks all accesories in the character and deletes only the ones that match yout Attachment list. Be sure to connect the function with your click detector part.

local Attachment = {
	"HairAttachment",
	"FaceAttachment",
	"FaceFrontAttachment",
	"HatAttachment",
	
	"WaistAttachment",
	"ShoulderAttachment",
	"NeckAttachment",
	"FrontAttachment",
	"BackAttachment",
	"WaistBackAttachment",
	"BodyBackAttachment",
	"LeftShoulderAttachment",
	"RightShoulderAttachment"
}

local removeButt = game.Workspace.RemoveAccs.ClickDetector

local function removeAccesories(player)
	local char = player.Character
	for _, a in pairs(char:GetDescendants()) do -- Get all items in the character
		if a:IsA("Accessory") then -- if the item is an accessory then
			for _, b in pairs(a.Handle:GetChildren()) do -- get all items in the accessory's Handle
				if b:IsA("Attachment") then -- if the item is an Attachment then
					local checkAttachment = table.find(Attachment, b.Name) -- compare if the name of the attachment is in the Attachment list
					if checkAttachment then -- if the attachment is in the list then destroy the accessory
						print(a.Name)
						a:Destroy()
					end
				end
			end

		end
	end
end
removeButt.MouseClick:Connect(removeAccesories)

Your question should be asked “How to remove accessories by type” if I am correct.
You do that with the following code reliably:

-- getAccessoryType
local function getAccessoryType(accessoryInstance)
	local attachment = accessoryInstance.Handle:FindFirstChildOfClass("Attachment")
	local accessoryType = attachment.Name:match("(.+)Attachment")
	return accessoryType
end

-- GetAccessoriesByType
local function getAccessories(character,typeAr)
	local accessoriesP = {}
	local humanoid = character.Humanoid
	--Checking whether to remove all accessories including those of the Hat class.*
	if typeAr["All"] == true then
		accessoriesP = humanoid:GetAccessories()
	else
		local accessories = humanoid:GetAccessories()
		if (#accessories > 0) then
			for i,v in pairs(accessories) do
				local accessory = character:FindFirstChild(tostring(v))
				if v:IsA("Accessory") then

					local attachment = v:FindFirstChildWhichIsA("Attachment", true)
					if typeAr[tostring(attachment)] == true then
						table.insert(accessoriesP,accessory)
					end						
				end	
			end
		end	
	end
	return accessoriesP
end

local function removeAccessories(character,removingArray)
	
	local removingAccessories = getAccessories(character,removingArray)
	-- Character RemoveAccessories
	for _,acc in ipairs(removingAccessories) do
		acc:Destroy()

	end
end

--Create an array to specify what you want to remove

local removingArray = { -- bool toggles if included in array
		["All"] = false,
		-- HEAD
		["FaceCenterAttachment"] = false,
		["FaceFrontAttachment"] = false, 
		["HairAttachment"] = false, --X
		["HatAttachment"] = true,		
		-- TORSO
		-- UpperTorso
		["BodyBackAttachment"] =true,
		["BodyFrontAttachment"] = false,	
		["LeftCollarAttachment"] = false,
		["RightCollarAttachment"] = false,
		["NeckAttachment"] = false,
		-- LowerTorso
		["WaistBackAttachment"] = true,
		["WaistCenterAttachment"] = true,
		["WaistFrontAttachment"] = true,		
		-- ARMS
		-- Hands
		["LeftGripAttachment"] = false,
		["RightGripAttachment"] = false,
		-- UpperArms
		["LeftShoulderAttachment"] = false,
		["RightShoulderAttachment"] = false,	
		-- LEGS
		-- R6 only
		["LeftFootAttachment"] =  false,
		["RightFootAttachment"] = false,		
		-- HRP
		["RootAttachment"] = false,	
	}

--Use it on a character
removeAccessories(YOURCHARACTERREFERENCEHERE,removingArray)

Note: Although there is a prettier method through the HumanoidDescription system this is undesirable to me as i

Note Also: there is a module of this on the site, Accessory Module: Removing and allowing different accessory types, but as I am aware it does not include all the types, as I did comparing against the enums (as of Jan 2021).

As for the playing of animations, that is seperate, and you just have to load the animation onto the humanoid as a concurrent but different process.

2 Likes

This worked. Thanks for the assistance, I’ll keep this code in mind next time I have an issue similar!