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
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.
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.
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:
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)
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:
And this is some hair I put on:
I hope this works, nothing too much I’m just here to give something that might be useful.
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)
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
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
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()
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