Script cannot detect some attachments after using FindFirstChild

I am trying to make a clothing closet that will apply clothes, hats, and animations to a player, and remove some of the player’s existing hats.
This specific part of the script is supposed to check if a Handle in an accessory has a specific attachment. If it locates the corresponding attachment, the Accessory is deleted.

However, it only deletes some of the specified accessories. It deletes most of the other accessory types (Can’t confirm yet, I haven’t made an outfit to try all of these out) but the NeckAttachment accessories and LeftShoulderAttachment accessories do not get deleted.

I get no error or warning in the console when the block is clicked.
(Every other part of the script works as intended)

	local Handle = player.Character:FindFirstChildOfClass("Accessory").Handle

	local ConnectedToHead = (Handle:FindFirstChild("WaistAttachment") or (Handle:FindFirstChild("ShoulderAttachment") or (Handle:FindFirstChild("NeckAttachment") or
		(Handle:FindFirstChild("FrontAttachment") or (Handle:FindFirstChild("BackAttachment") or (Handle:FindFirstChild("WaistBackAttachment") or
		(Handle:FindFirstChild("BodyBackAttachment") or (Handle:FindFirstChild("LeftShoulderAttachment") or (Handle:FindFirstChild("RightShoulderAttachment")))))))))) ~= nil
		if ConnectedToHead then
		Handle.Parent:Destroy()
	end
--I know, it's a mess of a script. I'm not particularly a good programmer, I'm new to this stuff
Images



I can confirm, I did spell the attachment names correctly

So how would I get the script to detect the other attachments?

I’ve tried my best to find a solution on the DevForum, Google, Twitter, and YouTube so far. No luck.

Please let me know if you need more of the script or if you need more images of the issue!

Edit: It appears the same issue happens with HatAttachments and HairAttachments.

	--removes hat, face, and hair accessories to make room for the head
	--i know this is extremely messy but if it works i'm gonna use it
	local Handle = player.Character:FindFirstChildOfClass("Accessory").Handle

	local ConnectedToHead = (Handle:FindFirstChild("FaceAttachment") or (Handle:FindFirstChild("HairAttachment") or (Handle:FindFirstChild("FaceFrontAttachment") or (Handle:FindFirstChild("HatAttachment"))))) ~= nil
	if ConnectedToHead then
		Handle.Parent:Destroy()
	end```

So, you Character:FindFirstChildOfClass(“Accesory”).handle and find the attachments. But is it inside a loop?

for example: a bucle for in pairs from his descendants

for i, Instance in pairs(Character:GetDescendants()) do
if Instance:IsA(“Accesory”) then
Instance.Handle:FindFirstChild(Name)
end
end

or character:GetChildren() in pairs.
it depends on how you manage accessories

edit:
and it’s necessary use find the child with his name? if you only need find attachment then use findfirstchildofclass(“attachment”)

2 Likes

It is not in a loop, it works by click detector. My mistake, I should have said that in the original post.

It probably isn’t necessary, I wouldn’t know, but the script is supposed to remove all hats except for hats classified under Hats, Face, and Hair.

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