How do I remove certain accessories from a character?

Hi,

I want to remove certain accessories from the player when they join. The problem is that the script removes all the accessories. Is there a way to make this script only remove face accessories and hats?

game.Players.PlayerAdded:Connect(function(plr)

	plr.CharacterAppearanceLoaded:Connect(function()
		local humanoid = plr.Character:WaitForChild("Humanoid")
			for i, v in pairs(plr.Character:GetChildren()) do
				for _, accessory in ipairs(humanoid:GetAccessories()) do
					if v:IsA("Accessory") then
						v:Destroy()
					end
				end
			end

		end)
	end)
3 Likes

Yeh, I believe that since humanoid:GetAccessories will return all the accessories, you can’t find a particular one. In this case, you should loop through the character and check the attachment name.

Face Accessories : FaceFrontAttachment, FaceCenterAttachment
Hat Accessories: HatAttachment
Hair Accessories: HairAttachment

Code from scripting helpers post:

local attachmentTypes = {
	FaceCenterAttachment = true, -- boolean for what accessories you want to remove
	FaceFrontAttachment = true,
	HatAttachment = true,
	HairAttachment = false,
}

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAppearanceLoaded:Connect(function()
		local child = plr.Character:GetChildren()
		for i, v in pairs(child) do
			if v:IsA("Accessory") then
				local attachment = v:FindFirstChildWhichIsA('Attachment', true)  -- recursively looking for attachment
				if attachment and attachmentTypes[attachment.Name] then -- if attachment is found and attachments in "attachmentTypes" table is found
					v:Destroy()
				end
			end
		end
	end)
end)
21 Likes