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

How to remove accessory from player character?

Asked by 6 years ago

This script doesn't work when It comes to removing ClassName "Accessory", but It works when I tested It on ClassName "Part". How to fix it, and why It happens?

Find = char:GetChildren()
for i = 1,#Find do
      if Find[i].ClassName == "Accessory" then
    Find[i]:Destroy()
      end
end

3 answers

Log in to vote
1
Answered by 6 years ago

Don't use 'for i =' loop if you want to remove hats.

Use this method instead:

for _, v in pairs(char:GetChildren()) do 
    if v:IsA('Accessory') then 
        local accessory = v
        accessory:Destroy()
    end
end
Ad
Log in to vote
0
Answered by 6 years ago

The reason might be that your script is running before the character's appearance has loaded. To do that you add repeat wait() until Player.HasAppearanceLoaded

Player.HasAppearanceLoaded returns true or false.

game.Players.PlayerAdded:Connect(function(Player)
    Player.CharacterAdded:Connect(function(Character)
        repeat wait() until Player.HasAppearanceLoaded

        local Find = Character:GetChildren()

        for i = 1,#Find do
            if Find[i].ClassName == "Accessory" then
                print(Find[i])
                Find[i]:Destroy()
            end
        end
    end)
end)
Log in to vote
0
Answered by
Amiaa16 3227 Moderation Voter Community Moderator
6 years ago

You can use the Humanoid:RemoveAccessories() function.

plr.CharacterAdded:Connect(function(char)
    char:WaitForChild"Humanoid":RemoveAccessories()
end)

Or you can use an event called ChildAdded and check if the child is an Accessory

plr.CharacterAdded:Connect(function(char)
    char.ChildAdded:Connect(function(c)
        if c.ClassName == "Accessory" then
            game:GetService"Debris":AddItem(c, 0.1)
        end
    end)
end)

Answer this question