Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you remove hats from a character?

Asked by
mnaj22 44
7 years ago

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
0
I tested it by changing remove to destroy, still didn't work. mnaj22 44 — 7y
0
Is this script inside of the character? AstrealDev 728 — 7y

1 answer

Log in to vote
0
Answered by
cabbler 1942 Moderation Voter
7 years ago

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)

Ad

Answer this question