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

How do you remove hats from a character?

Well, I have a script that removes all Accessories from the character, but for some reason they player sometimes has it and sometimes doesn't, is there a better way to remove hats?

Stuff = script.Parent:GetChildren()
for _, thing in pairs (Stuff) do
	if thing.ClassName == "Accessory" then
		thing:remove()
	end
end

2 comments

0

I tested it by changing remove to destroy, still didn't work.

0

Is this script inside of the character?

1 answer

✓ Accepted answer

You have to first wait until the character loads its appearance before you remove anything. These may help: Player:HasAppearanceLoaded() , Player:ClearCharacterAppearance() , Player.CharacterAppearanceLoaded .

If you want to remove all accessories, that's easy. If you want to remove only literal "hats" then you should destroy Hat objects, as well as accessories which have a "HatAttachment". Here is what I would do:

player.CharacterAppearanceLoaded:connect(function(char)
	for _,v in pairs(char:GetChildren()) do
		if v:IsA('Hat') or (v:IsA('Accessory') and v:WaitForChild('Handle'):FindFirstChild('HatAttachment'))  then
			v:Destroy()
		end
	end
end)

However I notice that this event can be inaccurate, so here is a possibly safer option. They both use the same check though.

character.ChildAdded:connect(function(v)
	if v:IsA('Hat') or (v:IsA('Accessory') and v:WaitForChild('Handle'):FindFirstChild('HatAttachment'))  then
		wait()
		v:Destroy()
	end
end)

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