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 6 years ago
Edited 6 years ago

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

01game.Players.PlayerAdded:Connect(function(player)
02    local char = player.Character
03    if char then
04        local count = char:GetChildren()
05        for i = 1, #count do
06            if (count[i].className == "Accessory") then
07                count[i]:Destroy()
08            end
09        end
10    end
11end)

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 — 6y

2 answers

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

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 6 years ago
Edited 6 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

01game.Players.PlayerAdded:Connect(function(player)
02    player.CharacterAdded:Connect(function(char)
03        local count = char:GetChildren()
04        for i = 1, #count do
05            if (count[i].className == "Accessory") then
06                count[i]:Destroy()
07            end
08        end
09    end)
10end)

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

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

Answer this question