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

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

Asked by
Shubu13 29
3 years ago

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) 
1
I know this is unrelated but you can do for i, v in pairs() instead of for i=1, #d do iRunzs 20 — 3y
0
Thank you for letting me know Shubu13 29 — 3y

2 answers

Log in to vote
1
Answered by 3 years ago

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.

Ad
Log in to vote
2
Answered by 3 years ago
Edited 3 years ago
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!

1
I learnt something new here, haha; I didn't know only hats had hat attachments. radiant_Light203 1166 — 3y
0
umm idk how to accept 2 answers.... Shubu13 29 — 3y
0
You can only accept one answer DarkDanny04 407 — 3y
0
Accept Danny's answer! radiant_Light203 1166 — 3y

Answer this question