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

Why isn't my script for removing hats working correctly?

Asked by 5 years ago
Edited 5 years ago

I made a script so that when a player enters, its hats will be deleted. Here is the script:

game.Players.PlayerAdded:Connect(function(player)
    local char = player.Character
    if char then
        local count = char:GetChildren()
        for i = 1, #count do
            if (count[i].className == "Accessory") then
                count[i]:Destroy()
            end
        end
    end
end)

Also, click me to see where the script is in.

Hope someone can help me fix the problem!

0
Hi, thanks to those people who gave upvotes! :) HomieFirePGN 137 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago
game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAppearanceLoaded:Connect(function(char)
        for i, v in pairs(char:GetChildren()) do
            if v:IsA("Accessory") then
                v:Destroy()
            end
        end
    end)
end)

Wiki links : 1. https://developer.roblox.com/api-reference/event/Player/CharacterAppearanceLoaded 2. https://developer.roblox.com/api-reference/function/Instance/IsA

Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

When a player is added the char variable is defined very quickly, not allowing the character to load. So the hats remover script part doesn't execute.

You should use the player.CharacterAdded function which will be fired each time a player dies.

The fixed script would be

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(char)
        local count = char:GetChildren()
        for i = 1, #count do
            if (count[i].className == "Accessory") then
                count[i]:Destroy()
            end
        end
    end)
end)

If this answered your question then don't forget to click the Accept Answer button.

0
It still doesn't work HomieFirePGN 137 — 5y
0
So apparently it passes through the for loop but the if loop isnt doing its thing HomieFirePGN 137 — 5y

Answer this question