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

How do I remove player`s hat/hair?

Asked by 6 years ago
Edited 6 years ago

I am trying to remove players hat, or in my case, hair. But it doesnt seems to work. I have a morth type of thing that when you touch a brick (button) it makes my body invisible and sticks random bricks on the body to make an armor or something.

01function onTouched(hit)
02    local d = hit.Parent:GetChildren()
03    for i=1, #d do
04        if (d[i].className == "Hat") then
05            d[i]:remove()
06        end
07    end
08end
09 
10script.Parent.Touched:connect(onTouched)

How ever the hair stays on my head and doesn`t gets removed. Any ideas please? Thank you.

4 answers

Log in to vote
2
Answered by
Astralyst 389 Moderation Voter
6 years ago
Edited 6 years ago

https://wiki.roblox.com/index.php?title=API:Class/Accessory

Don't use "Hat", use "Accessory".

1if (d[i]:IsA("Accessory")) then
2    d[i]:Destroy()

also, use :IsA instead of .className ==

0
wait, className? Astralyst 389 — 6y
0
Oh thanks. Yeah I will fix it right now. I didnt know hair wasnt a "hat", I thought everything on your head is a hat. HeyItzDanniee 252 — 6y
0
Just a new comment. It worked. The morth removed my hair. Thank you astrawr. :D HeyItzDanniee 252 — 6y
View all comments (5 more)
1
Note that accessories aren't just hats! They are wings, and all those things you can put on your player. If you want to see for yourself, just press play and look in your player model! Adv3rtizement 101 — 6y
2
You should not be using remove in your code and Hats still exist but most use Accessories. User#5423 17 — 6y
0
You can debug that script by checking if the y value of the Accessory is more than the y value of the Character's head. Just something to think about. DeceptiveCaster 3761 — 6y
0
Bad what if there is an accessory that is not a hat nc2r 117 — 5y
0
Bad what if there is an accessory that is not a hat nc2r 117 — 5y
Ad
Log in to vote
1
Answered by
ee0w 458 Moderation Voter
6 years ago
Edited 6 years ago

You could iterate through the character and destroy every Accessory instance.

1for i, v in pairs(character:GetChildren()) do
2    if v:IsA("Accessory") then
3        v:Destroy()
4    end
5end

Be sure to Upvote/Accept if I helped!

0
This would remove all Accessories from the player not just hats. User#5423 17 — 6y
Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

Roblox converted hats to accessories so you should be using the new attachement system.

The new system works by attaching the same name attachment with the one in the players character so you cannot easily define what is a hat.

The best way would be to simple check which accessories use the attachments in the players head by checking the name.

Lastly do not use remove() as it is deprecated use Destroy()

Example only removing hats and accessories which use the players head attachments.

01-- list of attachment names to remove
02local nameList = {
03    FaceCenterAttachment = true,
04    FaceFrontAttachment = true,
05    HairAttachment = true,
06    HatAttachment = true
07}
08 
09script.Parent.Touched:Connect(function(hit)
10    for i, v in pairs(hit.Parent:GetDescendants()) do
11        if v:IsA('Accessory') then
12 
13            -- recursivly look for a child which is a Attachment
14            local attach = v:FindFirstChildWhichIsA('Attachment', true)
15 
View all 27 lines...

Hope this helps.

0
Best one yet. This is should be the one that's accepted. iSpaceRBLX 31 — 5y
Log in to vote
0
Answered by
RubenKan 3615 Moderation Voter Administrator Community Moderator
6 years ago

The humanoid has a method for this. It's Humanoid:ClearAllAccessories(). Use this instead of looping or anything else to keep your code nice and clean.

0
That's a thing? Couldn't find it anywhere on their wiki. Astralyst 389 — 6y
0
Heheh, whoops. RubenKan 3615 — 6y

Answer this question