Help removing accessories

I want to remove the accessories of all players that join and ran into a bit of an issue. This is my code (located in ServerScriptService), which iterates through the character instance. There are no errors but none of my accessories are not being removed. Here’s my code:

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		if char:FindFirstChild("Humanoid") then
			for i, v in pairs(plr:GetChildren()) do
				if v:IsA("Accessory") then
					v:Destroy()
				end
			end
		end
	end)
end)

I understand that I can achieve the same goal with this code, but want to know what is wrong with my other code for the purpose of learning.

Alternative Solution
game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAppearanceLoaded:Wait()
	plr.Character.Humanoid:RemoveAccessories()
end)

2 Major Things:

  1. You should use CharacterAppearanceLoaded instead of CharacterAdded just to make sure all accessories are loaded and can be removed.
  2. You are iterating over the player and not the character.
    for i, v in pairs(plr:GetChildren()) should be → for i, v in pairs(char:GetChildren())

But yeah that should be about it.

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAppearanceLoaded:Connect(function(char)
		if char:FindFirstChild("Humanoid") then
			for i, v in pairs(char:GetChildren()) do
				if v:IsA("Accessory") then
					v:Destroy()
				end
			end
		end
	end)
end)
3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.