Remove players hats excluding hair

You should be using string.find(), as it will find the word hair anywhere in the hat name, but this method isn’t good. As @Camper0008 has pointed out there is an Attachment called HairAttachment, so simply looking for that will be a solution.

local children = character:GetChildren()
for i=1, #children do
     local instance = children[i]
     if (instance.ClassName == "Accessory") then
          if (not instance.Handle:FindFirstChild("HairAttachment")) then --it's not hair!
               instance:Destroy()
          end
     end
end
5 Likes

What if you grabbed the item IDs of the player’s accessories, and call the :GetProductInfo() method of MarketplaceService with each ID, then see if the AssetTypeId equals 41 (which is HairAccessory)

I just tested this idea, and it worked like a charm.

8 Likes

yes, string.match does find it anywhere in the name

“character” has an error. (30 charsss)

1 Like

sorry if i’m being late but every type of accessory has it’s own “unique” position (try math.floor to determine more less where it is) on character for humanoidrootpart and we can very fastly determine what type of accessory it is. It might be inaccurate sometimes but it’s very fast.

1 Like

Thank you, that’s an awesome idea! Do you happen to know what is the ID for Face Accessories?

You could call the :GetAppliedDescription() method on the character’s humanoid to grab a HumanoidDescription object, which has properties such as the IDs of all FaceAccessories that they’re wearing:

https://developer.roblox.com/en-us/api-reference/class/HumanoidDescription

1 Like

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