The title says it all but I've been using this script and it removes everything not just hat.
function onTouched(hit) local d = hit.Parent:GetChildren() for i=1, #d do if (d[i].className == "Accessory") then d[i]:remove() end end end script.Parent.Touched:connect(onTouched)
As far as I am aware, hats are also just accessories. Is there a way to differentiate between two hats? Do they contain meshes which point to an asset on the Roblox catalog? If they do the easiest way would be to create a table that contains blacklisted IDs and stops them from being removed.
Although I'm not sure if the following would work, but I hope it will help.
local Blacklisted = { 1000001, 1000002, 1000003 } function IsBlacklisted(Accessory) for _, v in pairs(Blacklisted) do if Accessory.Mesh.MeshId == v return true else return false end end end -- somewhere in line four of your code if d[i].className == "Accessory" and not IsBlackListed(d[i]) then
But I doubt that we are using meshes, so in your case possibly manually add something that prevents it from being deleted, i.e a bool value which, when true, stops d[i] from being destroyed.
function onTouched(hit) local d = hit.Parent:GetChildren() for i=1, #d do if (d[i].className == "Accessory") then if d[i].Handle:FindFirstChild("HatAttachment") then d[i]:Destroy() end end end end script.Parent.Touched:connect(onTouched)
Hair and hats both have their own name for the attachment that keeps them on the player. In this case, only hats have the name "HatAttachment."
What this does is it checks if it is an accessory, and if it is, it goes into the handle and searches for a HatAttachment. If it finds any hats, it will then destroy them.
Also, :remove() is depreciated, so using :Destroy() can make the game run slightly faster.
Hopefully this helped!