Why won't my script remove the player's accessories?

I have a script that I made that is supposed to remove the player’s accessories, then put on their team’s clothes. And for some reason, it isn’t taking off their accessories.

local players = game:GetService("Players")


players.PlayerAdded:Connect(function(p)
	p.CharacterAdded:Connect(function(Char)

		for i,a in pairs(Char:GetChildren()) do

			if a.ClassName == "Accessory"  then

				a:Destroy()

			end
		end


		local Folder = script:FindFirstChild(p.Team.Name)
		if Folder then

			local Tools = Folder:FindFirstChild("Tools")
			if Tools then

				for i,v in pairs(Tools:GetChildren()) do

					v:Clone().Parent = p.Backpack
				end
			end

			local Clothes = Folder:FindFirstChild("Clothes")
			if Clothes then

				for i,v in pairs(Clothes:GetChildren()) do
					local Clothing = Char:FindFirstChild(v.Name)
					if Clothing then
						Clothing:Destroy()
					end
					v:Clone().Parent = Char
				end
			end
		end
	end)
end)
1 Like

Just use Humanoid:RemoveAccessories. Makes things a lot easier.

3 Likes

I did not realize that was a thing, thank you.

1 Like

I tried using it in a separate script and it worked but it would destroy the team hats too. I moved the exact same script into my team script to make it remove them before the team hats are added and now it doesn’t work. Do you know what’s wrong?

local players = game:GetService("Players")


local function playerAdded(player)
	player.CharacterAdded:Connect(function(character)
		
		
		
		local Folder = script:FindFirstChild(player.Team.Name)
		local humanoid = character:WaitForChild("Humanoid")

		humanoid:RemoveAccessories()
		
		if Folder then

			local Tools = Folder:FindFirstChild("Tools")
			if Tools then

				for i,v in pairs(Tools:GetChildren()) do

					v:Clone().Parent = player.Backpack
				end
			end

			local Clothes = Folder:FindFirstChild("Clothes")
			if Clothes then

				for i,v in pairs(Clothes:GetChildren()) do
					local Clothing = character:FindFirstChild(v.Name)
					if Clothing then
						Clothing:Destroy()
					end
					v:Clone().Parent = character
				end
			end
		end
	end)
end

players.PlayerAdded:Connect(playerAdded)

You may need to wait for the character’s appearance to load.

1 Like

That seemed to work, thank you. I didn’t realize mine had CharacterAdded and not CharacterAppearanceLoaded.

Just a little bit of a warning, when I used appearance loaded in the past if roblox doesn’t load the avatars for some reason, it can cause that to never run. So make sure nothing too important relies on it.

1 Like

The character’s appearance may load before you listen for CharacterAppearanceLoaded to fire.

local Game = game
local Players = Game:GetService("Players")

local function OnPlayerAdded(Player)
	local function OnCharacterAdded(Character)
		if not Player:HasAppearanceLoaded() then Player.CharacterAppearanceLoaded:Wait() end
		--Code.
	end
	Player.CharacterAdded:Connect(OnCharacterAdded)
end
Players.PlayerAdded:Connect(OnPlayerAdded)

https://developer.roblox.com/en-us/api-reference/function/Player/HasAppearanceLoaded