Remove only hats / Accessories

Hello :slight_smile:

I have this script in ServerScriptService, which allows me to try hats in my game, everything works fine, but I would like to replace “Humanoid: RemoveAccessories ()” in this script with something else so that I can remove only the hat and not the Hair. Is it possible ?

local ServerStorage = game:GetService("ServerStorage") 
local RealHats = ServerStorage.RealHats:GetChildren() 

local FakeHats = workspace:WaitForChild("FakeHats"):GetChildren() 

local function GiveHat(player,FakeHat) 
	
	local Character = player.Character or player.CharacterAdded:Wait() 
	local HatCheck = Character:FindFirstChild(FakeHat.Name) 
	local Humanoid = Character:WaitForChild("Humanoid") 
	
	Humanoid:RemoveAccessories() 
	
	if not HatCheck then 
		for i,RealHat in pairs(RealHats) do 
			if FakeHat.Name == RealHat.Name then 
				local ClonedHat = RealHat:Clone() 
				Humanoid:AddAccessory(ClonedHat) 
			end 
		end 
	end 
end

for i,FakeHat in pairs(FakeHats) do 
	local Handle = FakeHat:FindFirstChild("Handle") 
	local ClickDetector = Handle:FindFirstChildOfClass("ClickDetector") 
	
	if Handle and ClickDetector then 
		ClickDetector.MouseClick:Connect(function(player) 
			GiveHat(player,FakeHat) 
		end) 
	end 
end
4 Likes

You could loop through the character, and check if it is a hat, like so:

for i, v in pairs(character:GetChildren()) do
    if v:IsA("Hat") then -- Most modern hats are "Accessory"s now. A different method would be to set a custom attribute called Hats or something, and if true, delete that hat.
        v:Destroy()
    end
end

Keep in mind, this is an example. This code assumes all of your hats are a “Hat” instance.

1 Like

thank you but it does not work :confused:

If you browse the descendants of a HatAccessory, you can notice that it contains a HatAttachment. It can be useful to make the difference between accessories.

image

local Character = -- your character

for i, v in pairs(Character:GetChildren()) do
	if v:IsA("Accessory") then
		isahat = false
		for j, desc in pairs(v:GetDescendants()) do
			if desc:IsA("Attachment") and desc.Name == "HatAttachment" then
				isahat = true
			end
		end
		if isahat then
			print("This is a hat : ", v)
		else
			print("This is not a hat : ", v)
		end
	end
end

Here’s an example of script that displays wether the accessories of your character are real hats or not. Hope it helps. :slightly_smiling_face:

1 Like

thank you , and if I want to integrate this function in a gui button? a button that would only remove hats or accessories without removing the hair? how to write it?

Make a localscript in a gui button
Put this in the localscript
V

function leftClick()
    local Character = game.Workspace[game.Players.LocalPlayer.Name].Character -- You shouldn't do this, but I'm too lazy to do any other way

    for i, v in pairs(Character:GetChildren()) do
    	if v:IsA("Accessory") then
    		isahat = false
    		for j, desc in pairs(v:GetDescendants()) do
    			if desc:IsA("Attachment") and desc.Name == "HatAttachment" then
    				isahat = true
    			end
    		end
    		if isahat then
    			print("This is a hat : ", v)
    		else
    			print("This is not a hat : ", v)
    		end
    	end
    end
end

script.Parent.MouseButton1Click:Connect(leftClick)

I don’t know how to make it not remove hair.
Also, this isn’t tested, so it might not work.

There is how I done it:

local Players = game:GetService("Players") 

Players.PlayerAdded:Connect(function(Player) 
Player.CharacterAdded:Connect(function(char) 
local Humanoid = char:WaitForChild("Humanoid") 
local DescriptionClone = Humanoid:GetAppliedDescription() 
DescriptionClone.HatAccessory = 0
Humanoid:ApplyDescription(DescriptionClone) 
end) 
end) 

Lemme know if it works for you as well!

1 Like

for the gui button? because I tried it doesn’t work

This code is not for the “gui button” but you get the point it is for removing the accessory itself when the player joins you can customize it to your likings :).

local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
    for i, v in pairs(player.Character:GetDescendants()) do
        if v.Name == "HatAttachment" then
            v.Parent.Parent:Destroy()
        end
    end
end)

Since the attachment is in an Handle which is in an accessory with Parent.Parent you are getting the accessory object and destroying it.

GetDescendants gets all the elements (parts’ children too) and with the for loop you check if the descendant name is “HatAttachment”, just in case you didn’t know :smiley:

1 Like

<code>

You should be able to do this by getting the Humanoid/Accessories and check if the accessories name is the same as the hats name and then use Parent to remove it.
Something like this:
for _, v in pairs(Character:GetChildren()) do
if v.Name == “Humanoid” then
local accessories = v:GetChildren()
for _, a in pairs(accessories) do
if a.Name == hatName then
a.Parent = nil
end
end
end
end

This is what I use on my games:

local function RemoveAccessories(Character, AttachmentTable)
	for Index, Accessory in pairs(Character:GetChildren()) do
		if Accessory:IsA("Accessory") then
			for Index, Attachment in pairs(Accessory.Handle:GetChildren()) do
				if Attachment:IsA("Attachment") and table.find(AttachmentTable, Attachment.Name) then
					Accessory:Destroy()
				end
			end
		end
	end
end

AttachmentTable is a table of strings, how to use the function: RemoveAccessories(Character, {"FaceCenterAttachment", "FaceFrontAttachment", "HatAttachment, "HairAttachment"})

1 Like