Removing all children which are accessories

Hello. I am trying to make a script which removes all accessories after the player joins the game.

I made this script but i realised that this will delete everything inside the character but the script didnt work either way so.

game.Players.PlayerAdded:Connect(function(plr)
	for index, child in pairs(plr.Character:GetChildren()) do
		child:Destroy()
	end
end)

``

So how do i make this script work the way i requested above?
1 Like

Just use the :IsA() method.

if child:IsA("Accessory") then
   child:Destroy()
end

Also make sure to wait for the character to load.

local character = player.Character or player.CharacterAdded:Wait()

So this would work?

game.Players.PlayerAdded:Connect(function(plr)
	local character = player.Character or player.CharacterAdded:Wait()
	for index, child in pairs(plr.Character:GetChildren()) do
		if child:IsA("Accessory") then
			child:Destroy()
		end
	end
end)

2 Likes

Yes, this will work as intended, and will also wait for the player character to load, so we don’t get any errors.

Make sure to change player to plr, as you saved the Player in a variable with a different name.

When i tested it, it didnt work. I still had all my accessories!

You need to also make some changes to the code, you need to replace what is inside pairs() with character:GetChildren().

Also, as I said above, change the player variable name.

It already is?

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(character)
		for index, child in pairs(plr.Character:GetChildren()) do
			if child:IsA("Accessory") then
				child:Destroy()
			end
		end	
	end)
end)

That way it will probably work as intended too, btw i recommend you changing plr.Character inside pairs() to character just for better code organization.

1 Like

Are you guys still discussing the problem because if so, I can help you get the best solution unless you want to mark it as solution.

I tested it out it worked for me though