IsA not working

I’m trying to have all accessories except hair be deleted and I made a script and put it into the StarterPack folder.

local char = script.Parent.Parent.Character
for _,v in pairs(char:GetChildren()) do
	print("1")
	if v:IsA("Accessory") then
		print("2")
		if v.AccessoryType:IsA("Hair") then
			print("3")
		else
			v:Destroy()
			print("4")
		end
	end
end

Am I missing something or what? It only prints the 1.
image

1 Like

Maybe try to put the script in StarterCharacterScript?

local char = script.Parent
for _,v in pairs(char:GetChildren()) do
	print(v)
	if v:IsA("Accessory") then
		print("2")
		if v.AccessoryType:IsA("Hair") then
			print("3")
		else
			v:Destroy()
			print("4")
		end
	end
end

Good idea! I did that and it’s not detecting the accessories for some reason? Now I’m at a complete loss.

1 Like

Try what I suggest now [ edited my comment], it should work

Still didn’t work, I’ll try a wait.

:IsA is for instance, AccessoryType is a property of acessory and it gives you an enum value. if v.AccessoryType ~= Enum.AccessoryType.Hair then v:Destroy end

1 Like

Thank you! But there’s still the issue of accessories not being detected with IsA in the first place.

Oops I forgot, I think you have to use a Enum instead of IsA.

ex;

acc.AccessoryType = Enum.AccessoryType.Hat
1 Like

This doesn’t work either, it’s the same issue as with my script.

1 Like

can you check if char is actually a character by printing char

1 Like

It is printing the character correctly.

1 Like
local char = script.Parent.Parent.Character
for _,v in pairs(char:GetChildren()) do
	print("1")
	if v:IsA("Accessory") then
		print("2")
		if v.AccessoryType == Enum.AccessoryType.Hair then
			print("3")
		else
			v:Destroy()
			print("4")
		end
	end
end

Also, if you’re interested Humanoid’s have a GetAccessories function which should only return accessories.

I also noticed something, make sure you are finding the accessories within a ServerScript, The code will unfortunately not run when using a ClientScript.

I started testing out some test code that detects the accessories in a ServerScript, and it seems to be working fine.

I’ve been using a Server script. As for now this is what my code looks like and it’s only deleting the hair?

local char = script.Parent

wait(6)
for _,acc in pairs(char:GetChildren()) do
	if acc:IsA("Accoutrement") then
		print("2")
		if acc.AccessoryType ~= Enum.AccessoryType.Hair then
			print("3")
		else
			acc:Destroy() 
			print("4")
		end
	end
end
1 Like
if acc.AccessoryType ~= Enum.AccessoryType.Hair then

Should be

if acc.AccessoryType == Enum.AccessoryType.Hair then

If you only want to keep the hair.

3 Likes
local char = script.Parent
--Server Script in StarterCharacterScripts
char.ChildAdded:Connect(function(c)
if c.ClassName=="Hat" or c.ClassName=="Accessory" then
c:Destroy()
end
end)

Try this. It will remove all hats.

1 Like

I still don’t know the exact issue but I got it fixed.

local char = script.Parent

wait(6)
for _,acc in pairs(char:GetChildren()) do
	if acc:IsA("Accoutrement") then
		if acc.AccessoryType ~= Enum.AccessoryType.Hair then
			acc:Destroy()
		end
	end
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.