Accessory Module: Removing and allowing different accessory types

Introduction
I am often asked how to distinguish and remove specific accessory types which are equipped by a Humanoid; whether they are a Hat, Hair, Face, Neck, Shoulders, Front, Back & Waist accessories. Hence, I have released this module which filters accessories based on the needs set by the developer.

https://www.roblox.com/library/4519330430/Accessory-Module

Observation
After analyzing various accessories of each type I have concluded that the ideal way to distinguish each accessory type is by the name of its child Attachment. The deciding factor for going with this solution came after finding out that for example a Shoulder accessory type can be comprised of different attachment types such as, NeckAttachment, LeftCollarAttachment, RightCollarAttachment depending on the need for the model of that specific accessory. Moreover, this module distinguishes the Accessory class from the Hat class too.

How to use this module?

  1. Put the ModuleScript in any container of your choice; for this example I will be putting it in the ServerStorage container.
  2. In the ModuleScript, set up the toggles table. (Further explanation regarding this table below.)
  3. Then require the ModuleScript from a Script and call the module:Filter(character) function from the Script whilst passing a Humanoid character. (Example of a use case below.)

How the structure should look like
image

Understanding the toggles table
["All"] refers to all accessories; both the Accessory and the Hat class.
["Accessory"] refers to all the accessories; specifically of the Accessory class.
["Hat"] refers to all the hats; specifically of the Hat class.
[AttachmentType.Name] = true/false;
false refers to disable the accessory/hat or accessories/hats.
true or the removal of the array from the table refers to allow the accessory/hat or accessories/hats.

Example on a character who has spawned via the CharacterAdded event

local accessory = require(game.ServerStorage.Accessory)
game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		game:GetService("RunService").Heartbeat:Wait()
		accessory:Filter(character)
	end)
end)

Source code

local module = {}

local toggles = {
	["All"] = true,
	["Accessory"] = true,
	["Hat"] = true,
	["BodyBackAttachment"] = false, --Removing BodyBackAttachments which tend to be of the Back Accessory type.
	["BodyFrontAttachment"] = true,
	["FaceFrontAttachment"] = true,
	["HairAttachment"] = true,
	["HatAttachment"] = true,
	["LeftCollarAttachment"] = true,
	["NeckAttachment"] = true,
	["RightCollarAttachment"] = true,
	["WaistCenterAttachment"] = true
}

--module:Filter(character)
--i.e. filtering accessories equipped to the Humanoid based on the toggles table.
function module:Filter(character)
	local humanoid = character.Humanoid
	--Checking whether to remove all accessories including those of the Hat class.
	if toggles["All"] == false then
		humanoid:RemoveAccessories()
	else
		local accessories = humanoid:GetAccessories()
		if (#accessories > 0) then
			for i,v in pairs(accessories) do
				local accessory = character:FindFirstChild(tostring(v))
				if v:IsA("Accessory") then
					--Checking whether to remove all accessories of the Accessory class.
					if toggles["Accessory"] == false then
						v:Destroy()
					--Checking whether to remove an accessory based on its Attachment class.
					else
						local attachment = v:FindFirstChildWhichIsA("Attachment", true)
						if toggles[tostring(attachment)] == false then
							accessory:Destroy()
						end						
					end
				--Checking whether to remove all accessories of the Hat class.
				elseif v:IsA("Hat") then
					if toggles["Hat"] == false then
						accessory:Destroy()
					end					
				end
			end
		end	
	end
end

return module
45 Likes

Do you have any idea why this doesn’t work for R6?

probably becuase You have to have the attachments in your starter character assuming you have a custom one OR your game do be buggin probs id look into it a bit more though if you havent cuz it works fine for me just saying…

I do hope this helped a little for you if at all to help you uncover your issue with it…