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

Why isn't this remove hat script working?

Asked by 8 years ago

This script is supposed to remove all hats from a player when a player joins:

game.Players.PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(character)
        local d = character:GetChildren()
        for i = 1, #d do
            if d[i].ClassName == "Hat" then
                d[i]:Destroy()
            end
        end     
    end)
end)

It doesn't work, though. Any help?

0
This is kinda a request, figure out where the error is next time and then we can fix that error. Conmmander 479 — 8y
0
This question is fine, the code is both simple and short. If it was like 100 lines then you'd be totally right, though. Perci1 4988 — 8y

1 answer

Log in to vote
1
Answered by
Rhyles 80
8 years ago

Your code isn't working because hats don't get added with the character, there is sometimes some delay between the character model being added and the hats being added to that mode. This code will avoid that delay:

game.Players.PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(character)
        character.ChildAdded:connect(function(child)
            if child:IsA("Hat") then
                wait()
                child:Destroy()
            end
        end) 
    end)
end)
Ad

Answer this question