Remove players hats excluding hair

You can use this module to remove the players hats excluding the hair: Accessory Module: Removing and allowing different accessory types

I do think that there is a property called “ClassItem” you can try to detect that to know that its a hair

This is quite easy to do actually! i have gone around it and found out that hair accessories use HairAttachments when they load into the player’s character, so you can just check if the player’s character has accessories that dont have this when they join the game, and delete them:

local Players = game:GetService("Players")

function PlayerAdded(Player)
Player.CharacterAppearanceLoaded:Connect(function(Character)
local parts = Character:GetChildren()
	for i = 1, #parts do
		local instanceA = parts[i]
		if (instanceA.ClassName == "Accessory") then
			if not instanceA.Handle:FindFirstChild("HairAttachment") then
				parts[i]:Destroy()
			end
		  end
	    end
     end)
end

Players.PlayerAdded:Connect(PlayerAdded)
9 Likes

Check the attachment position. This is really the only way to make sure it is hair, otherwise its impossible.

If I’m not mistaken, I’m pretty sure there is a thing for this.
if I click the accessory in the explorer, There is something called “AccessoryType”
This is a hat I have put on:
image_2021-12-11_152159

And this is some hair I put on:
image_2021-12-11_152251

I hope this works, nothing too much I’m just here to give something that might be useful.

This is impossible, unless you check every single hair texture in the script, but that’s impossible.

There is a possible way, try this.

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(char)
        repeat wait() until player.Character
        for _, v in pairs(game.Workspace[player.Name]:GetChildren()) do 
            if v:IsA('Accessory') and not string.match(v.Name:lower(), "hair") then 
                local accessory = v
                accessory:Destroy()
            end
        end
    end)
end)
1 Like

In addition to this solution by @Phromes :

As mentioned here’s the docs for AccessoryTypes

Here’s a script I wrote that strips all accessories from a character except for the hair.

function dressCharacter(character: Model)
	for _, value in pairs(character:GetChildren()) do
		if value.ClassName == 'Accessory' then
			if value.AccessoryType ~= Enum.AccessoryType.Hair then
				value:Destroy()
			end
		end
	end
end
1 Like

You could create a function specifically for this, something like this:
–Input the player into the function:
function removeHats(player)
–Get the character of the player:
local char = player.Character
–If the character doesn’t exist, return:
if not char then return end
–Get the Head of the character:
local head = char:WaitForChild(“Head”)
–If the Head doesn’t exist, return:
if not head then return end
–Loop through the children of the Head:
for i,v in pairs(head:GetChildren()) do
–Check if the child is a hat:
if v:IsA(“Hat”) then
–Destroy the hat:
v:Destroy()
end
end
end

game.Players.PlayerAdded:Connect(function(Player)

Player.CharacterAdded:Connect(function(Character)
	wait(6) --  the reason i added this is because accessorys take a while to be added 
	for i, item in pairs(Character:GetChildren()) do
		
		if item:IsA("Accessory") then
			for i, Handlechild in pairs(item.Handle:GetChildren()) do
				if Handlechild:IsA("Attachment") then -- the reason this is here is because the if statement following its will become true 
					
					if Handlechild.Name ~= "HairAttachment" then
                        
						item:Destroy()
						end
				end

			end
		end
	end

	Character.Humanoid.Died:Connect(function()
		wait(3)
		Player:LoadCharacter()
	end)
end)

Player:LoadCharacter()

end)

–here’s a better script

if v:IsA("Accessory") then
   if not string.find(v.Name:lower(), "hair") then
        v:Destroy()
   end
end

There is a way to tell if a player has a hat accessory or hair. Inside a hair accessory there is a part called “Handle”. And inside handle is a attachment called “HairAttachment”, while other accessorys have attachments called something else. You can use this to exclude hair accessorys. Like this for example,

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()


wait(1)
for i, v in pairs(char:GetChildren()) do
	if v:IsA("Accessory") and not v:WaitForChild("Handle"):FindFirstChild("HairAttachment") then
		v:Destroy()
	end
end