Troubles removing character accessories via a script

I’m trying to remove some accessories from the selected player, but only their hats and hairs (as if their character was bald). It would run successfully prior to adding the lines which check r[i].AccessoryType so I believe the issue is there but there’s no errors in console??

local char = game.Workspace:FindFirstChild(player.Name)
local r = char:GetChildren()
if character:GetAttribute("NoAccessory") == nil  or character:GetAttribute("NoAccessory") == false then
	for i=1, #r do
		if (r[i].className == "Accessory") then
			if r[i].AccessoryType == "Hat" or r[i].AccessoryType == "Hair" then
				r[i]:remove() 
			end
		end
	end
	character:SetAttribute("NoAccessory", true)
end

Any help would be appreciated. (I’m also aware there’s probably many quicker and more effective ways to write this lol)

2 Likes

on first line, please use WaitFortChild instead of FindFirstChild just to be sure

.className should be .ClassName with upper C.

use :Destroy instead of :remove

1 Like

Accessory.AccessoryType uses Enums, so your code would have to look like this:

if r[i].AccessoryType == Enum.AccessoryType.Hat or r[i].AccessoryType ==  Enum.AccessoryType.Hair then
	r[i]:Destroy() 
end
1 Like

Thank you for the help!

Have only used Enums in small amounts previously for keyboard inputs so I am relatively unfamiliar, can you give a brief explanation / link documentation where I could learn a little more??