Script cannot detect some attachments after using FindFirstChild

if you use Attachment = handle:findfirstchild(“name”) or handle:findfirstchild(“name2”) then would set the variable that is true (sorry for my english)
example:
if name1 not exist but exist name2 and use or in a scope (attachment) then attachmen = name2

I’m sorry, i’m not exactly sure what you mean

the problem is that you cannot destroy all Instances that do you like?

if you use multiples findfirstchild only return the first child that has founded

local attachment = handle:FindFirstChild(“name1”) or Handle:FindFirstChild(“Handle2”)
what happened if both exist? only one can be attachment variable

so, if findfirstchild(“name1”) exist then handle:findfirstchild(“name2”) would be ignored
that is work “or” in the variable (attachment)

if you use Character:FINDFIRSTchildofCLASS(“accesory”) only return the first accesory that has been founded inside character, so if you have more of 3 accesory and you wanna delete it then use again findfirstchildofclass until every accesory that you like delete be destroyed

It’s still not deleting the other hats

can show your code? i need see what are coding to delate the accesorys

I still didn’t completely understand what you meant so I tried multiple different things:
Attempt #1:

-- I changed every "FindFirstChild" to "GetChildren" in an attempt to get the script to grab every Accessory of that type
	local Handle = player.Character:FindFirstChildOfClass("Accessory").Handle

	local ConnectedToHead = (Handle:GetChildren("WaistAttachment") or (Handle:GetChildren("ShoulderAttachment") or (Handle:GetChildren("NeckAttachment") or
		(Handle:GetChildren("FrontAttachment") or (Handle:GetChildren("BackAttachment") or (Handle:GetChildren("WaistBackAttachment") or
			(Handle:GetChildren("BodyBackAttachment") or (Handle:GetChildren("LeftShoulderAttachment") or (Handle:GetChildren("RightShoulderAttachment")))))))))) ~= nil
		if ConnectedToHead then
		Handle.Parent:Destroy()
	end

Attempt #2:

-- I changed every "GetChildren" to "FindFirstChildOfClass"
	local Handle = player.Character:FindFirstChildOfClass("Accessory").Handle

	local ConnectedToHead = (Handle:FindFirstChildOfClass("WaistAttachment") or (Handle:FindFirstChildOfClass("ShoulderAttachment") or (Handle:FindFirstChildOfClass("NeckAttachment") or
		(Handle:FindFirstChildOfClass("FrontAttachment") or (Handle:FindFirstChildOfClass("BackAttachment") or (Handle:FindFirstChildOfClass("WaistBackAttachment") or
			(Handle:FindFirstChildOfClass("BodyBackAttachment") or (Handle:FindFirstChildOfClass("LeftShoulderAttachment") or (Handle:FindFirstChildOfClass("RightShoulderAttachment")))))))))) ~= nil
		if ConnectedToHead then
		Handle.Parent:Destroy()
	end

Attempt #3:

-- Same as attempt #2, but I duplicated the main body to double check and remove any secondary accessories left behind
	local Handle = player.Character:FindFirstChildOfClass("Accessory").Handle

	local ConnectedToHead = (Handle:FindFirstChildOfClass("WaistAttachment") or (Handle:FindFirstChildOfClass("ShoulderAttachment") or (Handle:FindFirstChildOfClass("NeckAttachment") or
		(Handle:FindFirstChildOfClass("FrontAttachment") or (Handle:FindFirstChildOfClass("BackAttachment") or (Handle:FindFirstChildOfClass("WaistBackAttachment") or
			(Handle:FindFirstChildOfClass("BodyBackAttachment") or (Handle:FindFirstChildOfClass("LeftShoulderAttachment") or (Handle:FindFirstChildOfClass("RightShoulderAttachment")))))))))) ~= nil
		if ConnectedToHead then
		Handle.Parent:Destroy()
	end
	
	local ConnectedToHead = (Handle:FindFirstChildOfClass("WaistAttachment") or (Handle:FindFirstChildOfClass("ShoulderAttachment") or (Handle:FindFirstChildOfClass("NeckAttachment") or
		(Handle:FindFirstChildOfClass("FrontAttachment") or (Handle:FindFirstChildOfClass("BackAttachment") or (Handle:FindFirstChildOfClass("WaistBackAttachment") or
			(Handle:FindFirstChildOfClass("BodyBackAttachment") or (Handle:FindFirstChildOfClass("LeftShoulderAttachment") or (Handle:FindFirstChildOfClass("RightShoulderAttachment")))))))))) ~= nil
	if ConnectedToHead then
		Handle.Parent:Destroy()
	end

None of these attempts worked.

I don’t think it’s an issue with multiple hats in the same category though, as my pauldron (which is a LeftShoulderAttachment) is the only LeftShoulderAttachment on my avatar, and it doesn’t get deleted.

Sorry if I’m being difficult or mistaking the obvious

how many accesorys do you have? 1? if you have just 1 you just need one findfirstchildofclass(“accesory”) but if you have more of one then you to use a bucle

if you know the number of accesorys you can do:
for i = 1, NumberOfAccesory do
local accesory = character:FindFirstChildOfClass(“Accesory”)
if Accesory then
–find the attachment with finding his class
local Attachment = accesory.Handle:FindFirstChildOfClass(“Attachment”)
if Attachment then
Attachment:Destroy()
end
end
end

Well the point of the script is it’s supposed to be able to accommodate any amount of hats
A player wearing 1 hat on their back should be able to use the clothing hanger the same as someone wearing 10 hats on their back

This means I can’t make the script specify my exact hats or else it would break when used with other outfits

explain me please, you need find every attachment?

Every Waist, Back, Front, Neck, and Shoulder accessory on the player should be deleted when the script is activated, including duplicates. The Face, Hat, and Hair accessories should remain on the player.

in attachment you put the name of every attachment that you need for destroy accesory

in #Character:GetChildren() return the number of childrens that have the character

Getting the error “Argument 1 missing or nil” after using the script.
image
The script deletes some accessories, but not all. (Pauldron for ex.)


The attachment name in the pauldron is LeftShoulderAttachment, which I input correctly as seen below

	local Attachment = {
		"WaistAttachment",
		"ShoulderAttachment",
		"NeckAttachment",
		"FrontAttachment",
		"BackAttachment",
		"WaistBackAttachment",
		"BodyBackAttachment",
		"LeftShoulderAttachment",
		"RightShoulderAttachment"
	}
	
local Character = player.Character
	
	local currentaccessory
	for i = 1, #Character:GetChildren(10) do
		currentaccessory = Character:FindFirstChildOfClass("Accessory")
		local findattachment = currentaccessory.Handle:FindFirstChild(Attachment[i])
		if findattachment then
			currentaccessory:Destroy()
	else
	print("No more accessories")
end
end```

where is the #attachment loop? you need two “for” bucle to index all accesories and other for index all strings of attachment Table/Array
without the bucle #Attachment can’t find every attachment

edit: The first bucle FOR do “for i = 1, #Character:GetChildren() do” and the bucle attachment put J or some name of variable “for k = 1, #Attachment do” (the array of strings that have all names of attachments)
and use K to index Attachment Array

Could you send a screenshot of your code or use a

code box

to paste code? It’s hard for me to read.

code box? i don’t know how i can do it

I don't know exactly what it's called but you
--use it like this

image

1 Like

Hiwi o/
I dont know which accesories you want to remove or not. So I just provide a little example of how to remove all accesories. By destroying them. You need to make customizations if you want only some specific accesories to be deleted

for _, a in pairs(game.Workspace.Beta_Protogen:GetDescendants()) do
	if a:IsA("Accessory") then
		a:Destroy()
	end
end

Paste and run that code on the command bar and all ur accesories should be deleted

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!