I made a script so that when a player enters, its hats will be deleted.
Here is the script:
01 | game.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 |
11 | end ) |
Also, click me to see where the script is in.
Hope someone can help me fix the problem!
1 | game.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 ) |
9 | end ) |
Wiki links :
1. https://developer.roblox.com/api-reference/event/Player/CharacterAppearanceLoaded
2. https://developer.roblox.com/api-reference/function/Instance/IsA
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
01 | game.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 ) |
10 | end ) |
If this answered your question then don’t forget to click the Accept Answer button.