Want to stay connected to the Scripting Helpers community and get real-time help? Join the Scripting Helpers Discord Server!
Ad
Log in to vote
2

How do I remove player`s hat/hair?

Asked by 8 days ago
Edited 8 days 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.

function onTouched(hit) 
    local d = hit.Parent:GetChildren() 
    for i=1, #d do 
        if (d[i].className == "Hat") then 
            d[i]:remove() 
        end 
    end
end 

script.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
1
Answered by
astrawr 222
8 days ago
Edited 8 days ago

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

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

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

also, use :IsA instead of .className ==

0
wait, className? astrawr 222 — 8d
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 47 — 8d
0
Just a new comment. It worked. The morth removed my hair. Thank you astrawr. :D HeyItzDanniee 47 — 8d
View all comments (4 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 63 — 8d
2
You should not be using remove in your code and Hats still exist but most use Accessories. kingdom5 3643 — 8d
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. MCAndRobloxUnited 437 — 7d
0
If classes are in there that inherit from accessory they will be destroyed since they are “IsA” accessories. Use ClassName to destroy a specific class. incapaz 1018 — 7d
Ad
Log in to vote
1
Answered by 8 days ago
Edited 8 days ago

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

for i, v in pairs(character:GetChildren()) do
    if v:IsA("Accessory") then
        v:Destroy()
    end
end

Be sure to Upvote/Accept if I helped!

0
This would remove all Accessories from the player not just hats. kingdom5 3643 — 8d
Log in to vote
1
Answered by
kingdom5 3643 Snack Break
8 days ago
Edited 8 days 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.

-- list of attachment names to remove
local nameList = {
    FaceCenterAttachment = true,
    FaceFrontAttachment = true,
    HairAttachment = true,
    HatAttachment = true
}

script.Parent.Touched:Connect(function(hit)
    for i, v in pairs(hit.Parent:GetDescendants()) do
        if v:IsA('Accessory') then

            -- recursivly look for a child which is a Attachment
            local attach = v:FindFirstChildWhichIsA('Attachment', true)

            -- check that the name is in the list ie only destroy hats
            if attach and nameList[attach.Name] then
                v:Destroy()
                print('print removed', v)
            end

        -- legacy check some models use old hats
        elseif v:IsA('Hat') then
            v:Destroy()
        end
    end
end)

Hope this helps.

Log in to vote
0
Answered by
RubenKan 2915 Administrator
7 days 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. astrawr 222 — 7d
0
Heheh, whoops. RubenKan 2915 — 6d

Answer this question