Introduction
AccessoryService allows you to do many accessory related tasks. This includes removing large avatar accessories, fetching a player’s accessory data, and giving a player accessories.
Documentation is included in the module script
This is my first resource! Any suggestions on post and documentation formatting would be greatly appreciated!
Why Use AccessoryService?
AccessoryService has many benefits such as:
- User-friendly and can be edited easily.
- Provides a simplified way of managing Accessories.
- Can be used for improved gameplay experience(e.i. removing large items)
Other Notes:
- Feel free to edit the module as you please!
- Inside the module, you can change the variable “PrintSettings” to true to allow you to see various processes in action.
- I’m open for suggestions for future updates and please report any bugs!
Example Code
This script uses AccessoryService to remove a player’s oversized accessories when they join the game!
-- This script will not save removed accessories upon death
local AccessoryService = require(game.ServerScriptService.AccessoryService)
local PlayerService = game:GetService("Players")
PlayerService.PlayerAdded:Connect(function(player)
local MaxSize = 2
task.wait(2)
AccessoryService.CheckPlayer(player, MaxSize)
end)
Copy and Paste Code Below For Module:
--{{INFORMATION}}--
--[[
{Lalot}
-----{AccessoryService}--{BETA}-----
**Devforum Documentation**
https://devforum.roblox.com/t/accessoryservice-delete-big-accessories-and-more/3067639
**Functions**
[AccessoryService.MeshIdFromAccessory]
>Description: Retrieves the MeshId from the given accessory.
>Parameters: Accessory (Instance) - The accessory instance to get the MeshId from.
>Returns: string - The MeshId of the accessory, or nil if not found.
[AccessoryService.TextureIdFromAccessory]
>Description: Retrieves the TextureID from the given accessory.
>Parameters: Accessory (Instance) - The accessory instance to get the TextureID from.
>Returns: string - The TextureID of the accessory, or nil if not found.
[AccessoryService.GetMeshVolume]
>Description: Calculates the volume of the mesh within the given accessory.
>Parameters: Accessory (Instance) - The accessory instance to calculate the volume for.
>Returns: number - The volume of the mesh.
[AccessoryService.CheckPlayer]
>Description: Checks all accessories on a player and removes those exceeding a specified volume.
>Parameters: Player (Player) - The player to check.
>MaxVolume (number) - The maximum allowed volume for accessories.
[AccessoryService.RemoveAccessories]
>Description: Removes all accessories from the specified player.
>Parameters: Player (Player) - The player from whom to remove all accessories.
[AccessoryService.RemoveClassic]
>Description: Removes all classic (non-layered) accessories from the specified player.
>Parameters: Player (Player) - The player from whom to remove classic accessories.
[AccessoryService.RemoveLayered]
>Description: Removes all layered accessories from the specified player.
>Parameters: Player (Player) - The player from whom to remove layered accessories.
[AccessoryService.LoadAccessory]
>Description: Loads an accessory from an asset ID and parents it to the specified parent.
>Parameters: AssetId (number), Parent (Instance)
>Returns: Instance - The loaded accessory.
[AccessoryService.RemoveSpecific]
>Description: Removes a specific accessory by asset ID from the specified player.
>Parameters: AssetId (number) - The asset ID of the accessory to remove.
>Player (Player) - The player from whom to remove the specific accessory.
[AccessoryService.GiveToPlayer]
>Description: Gives a specific accessory by asset ID to the specified player.
>Parameters: AssetId (number) - The asset ID of the accessory to give.
>Player (Player) - The player to whom to give the accessory.
[AccessoryService.ListAccessories]
>Description: Lists all accessories a player currently has.
>Parameters: Player (Player) - The player whose accessories are to be listed.
>Returns: table - A table containing the names of all accessories the player has.
[AccessoryService.GetAllHatIDs]
>Description: Retrieves all hat asset IDs from a player's accessories.
>Parameters: Player (Player) - The player whose hat IDs are to be retrieved.
>Returns: table - A table containing all hat asset IDs.
[AccessoryService.GetAllHairIDs]
>Description: Retrieves all hair asset IDs from a player's accessories.
>Parameters: Player (Player) - The player whose hair IDs are to be retrieved.
>Returns: table - A table containing all hair asset IDs.
]]
----- PROGRAM -----
local AccessoryService = {}
-- Settings
local PrintResults = true
----- FUNCTIONS -----
-- Get Mesh Id from Accessory
function AccessoryService.MeshIdFromAsseccory(Accessory)
if not Accessory or not Accessory.Handle then return end
-- Grab Handle Instance From Accessory
local Mesh = Accessory.Handle
if Mesh then
return Mesh.MeshId
end
end
-- Get Texture Id from Accessory
function AccessoryService.TextureIdFromAccessory(Accessory)
if not Accessory or not Accessory.Handle then return end
-- Grab Handle Instance From Accessory
local Mesh = Accessory.Handle
if Mesh then
return Mesh.TextureID
end
end
-- Calculate Volume of Mesh
function AccessoryService.GetMeshVolume(Accessory)
-- Get Size
local OriginalSize
-- Uses Original Size if Normal, Handle Size if Cageable
if not Accessory.Handle:FindFirstChildWhichIsA("WrapLayer") then
OriginalSize = Accessory.Handle.OriginalSize.Value
else
OriginalSize = Accessory.Handle.Size
end
local X = OriginalSize.X
local Y = OriginalSize.Y
local Z = OriginalSize.Z
-- Calculate Volume
local Volume = X*Y*Z
-- Print Results
if PrintResults == true then
print(tostring(Accessory) .. "'s volume is around: " .. tostring(math.floor(Volume)))
end
return Volume
end
-- Check Player Hats
function AccessoryService.CheckPlayer(Player, MaxVolume: number)
-- Grab Players Character
local Character = workspace:WaitForChild(Player.Name)
--[[1. Loops through player's character
2. Find Accessories
3. Deletes If necessary]]
for _,v in pairs(Character:GetChildren()) do
if v:IsA("Accessory") then
-- Checks If Accessory is a Layerd Clothing Piece
--if v.Handle:FindFirstChildWhichIsA("WrapLayer") and IncludeCageable == false then continue end
-- Get Properties of Mesh
local MeshVolume = AccessoryService.GetMeshVolume(v)
-- Check Threshold, Then Deletes If Necessary
if MeshVolume > MaxVolume then
-- Print Results
if PrintResults == true then
print(tostring(v) .. "'s volume has exceeded given max volume. It has been deleted.")
end
v:Destroy()
end
end
end
end
-- Removes All Player's Accessories(including Layered Clothing)
function AccessoryService.RemoveAccessories(Player)
-- Grab Players Character
local Character = workspace:WaitForChild(Player.Name)
-- Loops through player's character
for _,v in pairs(Character:GetChildren()) do
if v:IsA("Accessory") then
v:Destroy()
end
end
end
-- Only Removes Player's Classic Accessories
function AccessoryService.RemoveClassic(Player)
-- Grab Players Character
local Character = workspace:WaitForChild(Player.Name)
-- Loops through player's character
for _,v in pairs(Character:GetChildren()) do
if v:IsA("Accessory") then
-- Checks If Accessory is a Layerd Clothing Piece
if v.Handle:FindFirstChildWhichIsA("WrapLayer") then continue end
v:Destroy()
end
end
end
-- Only Removes Cageabled Accessories
function AccessoryService.RemoveLayered(Player)
-- Grab Players Character
local Character = workspace:WaitForChild(Player.Name)
-- Loops through player's character
for _,v in pairs(Character:GetChildren()) do
if v:IsA("Accessory") then
-- Checks If Accessory is a Layerd Clothing Piece
if not v.Handle:FindFirstChildWhichIsA("WrapLayer") then continue end
v:Destroy()
end
end
end
-- Import Accessory From Id
function AccessoryService.LoadAccessory(AssetId: number, Parent)
-- Loads Asset
local InsertedModel = game:GetService("InsertService"):LoadAsset(AssetId)
local Accessory
-- Takes out Accessories from Inside Model
for _,v in pairs(InsertedModel:GetChildren()) do
v.Parent = Parent
v.Handle.Locked = false
-- Sets Accessory to v to be Returned
Accessory = v
end
InsertedModel:Destroy()
return Accessory
end
-- Removes Specific Item
function AccessoryService.RemoveSpecific(AssetId: number, Player)
-- Grab Players Character
local Character = workspace:WaitForChild(Player.Name)
-- Creates Instance For Checking
local TestHat = AccessoryService.LoadAccessory(AssetId, game.ServerStorage)
-- Loops Through Character, Deletes Hats
for _,v in pairs(Character:GetChildren()) do
if v:IsA("Accessory") then
if v.Name == TestHat.Name then
v:Destroy()
end
end
end
end
-- Gives Item To Player
function AccessoryService.GiveToPlayer(AssetId: number, Player)
-- Grab Players Character
local Character = workspace:WaitForChild(Player.Name)
-- Inserts Hat
local Hat = AccessoryService.LoadAccessory(AssetId, Character)
Hat.Parent = Character
end
-- Function to list all accessories a player has
function AccessoryService.ListAccessories(Player)
-- Ensure the player's character exists
local Character = workspace:WaitForChild(Player.Name)
local Accessories = {}
-- Iterate through the character's children
for _, v in pairs(Character:GetChildren()) do
if v:IsA("Accessory") then
table.insert(Accessories, v.Name)
end
end
return Accessories
end
-- Get All Hat Asset Ids From Accessory Object
function AccessoryService.GetAllHatIDs(Player)
-- Get Data
local HumanoidDescription = Player.Character.Humanoid.HumanoidDescription
local HatsList = HumanoidDescription.HatAccessory
print(tostring(HatsList))
-- Deserialize HatsList
local Deserialized = tostring(HatsList):split(",")
return Deserialized
end
-- Get All Hair Asset Ids From Accessory Object
function AccessoryService.GetAllHairIDs(Player)
-- Get Data
local HumanoidDescription = Player.Character.Humanoid.HumanoidDescription
local HairList = HumanoidDescription.HairAccessory
print(tostring(HairList))
-- Deserialize HatsList
local Deserialized = tostring(HairList):split(",")
return Deserialized
end
return AccessoryService