Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I delete a player's hats but not hair?

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) 

2 comments

1

I know this is unrelated but you can do for i, v in pairs() instead of for i=1, #d do

0

Thank you for letting me know

2 answers

✓ Accepted answer

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!

4 comments

1

I learnt something new here, haha; I didn't know only hats had hat attachments.

0

umm idk how to accept 2 answers....

0

You can only accept one answer

0

Accept Danny's answer!

This discussion is preserved as it was. New answers, comments, and votes are closed.