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

Help with hat removing?

Asked by
FiredDusk 1466 Moderation Voter
8 years ago

I want when I touch this part, all hats in my character get removed.

Part = script.Parent

Part.Touched:connect(function(hit)

    for i,v in pairs(hit.Parent:GetChildren()) do
        v.Transparency = 1
        if v:IsA("Hat") then 
            v:remove()

        end
    end 
end)

1 answer

Log in to vote
1
Answered by 8 years ago

First of all, the remove method is deprecated, you should be using destroy instead. Now, as to why this isn't working, you're attempting to set the Transparency property of all children of the character. Not all children of the character have a Transparency property, so you'll get an error when trying to set the property on a child that doesn't have it.

Here's a more robust version, tested and it's working:

script.Parent.Touched:connect(function(hit)
    -- Verify the thing that touched the part is actually a player
    if hit.Parent:FindFirstChild("Humanoid") then
        for i, v in pairs(hit.Parent:GetChildren()) do
            if v:IsA("Hat") then
                v:Destroy();
            end
        end
    end
end)
0
Whats about the transparency? FiredDusk 1466 — 8y
0
@PreyStar what exactly are you trying to do? Remove the hats and set the player's parts to transparent? DreadPirateRobux 655 — 8y
Ad

Answer this question