Help With Accessory Manager

Hello, recently i have been trying to make an accessory manager that display’s all of the local player’s accessories in a form of imagebuttons and their assigned asset thumbnails, and when a player clicks on the corresponding imagebutton it would remove the accessory.

But ive ran into an issue, and this that i dont know how to make the removing accessory part.

Here is my script:

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local button = script.Parent
local parentGui = button.Parent 

button.MouseButton1Click:Connect(function() 
	if humanoid then
		local humanoidDescription = humanoid:GetAppliedDescription()
		local accessoryTypes = {"HatAccessory", "HairAccessory", "FaceAccessory", "NeckAccessory", "ShouldersAccessory", "FrontAccessory", "BackAccessory", "WaistAccessory"}

		for _, accessoryType in ipairs(accessoryTypes) do
			local accessories = humanoidDescription[accessoryType]

			if accessories and accessories ~= "" then
				local accessoryIds = string.split(accessories, ",")

				for _, accessoryId in ipairs(accessoryIds) do
					local newButton = Instance.new("ImageButton") 
					newButton.Image = "rbxthumb://type=Asset&id=" .. accessoryId .. "&w=420&h=420" 
					newButton.Parent = parentGui.Frame.ScrollingFrame 

					
					newButton.MouseButton1Click:Connect(function()
						for _, accessory in ipairs(character:GetChildren()) do
							if accessory:IsA("Accessory") then
								local mesh = accessory:FindFirstChild("Handle")
								if mesh then
									local assetId = tonumber(mesh.MeshId:match("id=(%d+)")) 
									if assetId == tonumber(accessoryId) then
										accessory:Destroy()
										break 
									end
								end
							end
						end
					end)
				end
			end
		end
	end
end)

And here is a video of how to it works:

If someone could correct me on how to make the corresponding accessory destroy when you click on the corresponding imagebutton , it would be really heapfull.

1 Like

Couldn’t you make it so it cycles through the player’s character and finds all the accessories that way?

That way you still get all of the currently worn accessories while defining them for later use at the same time.

local Player = game.Players.LocalPlayer
local Character = Player.Character

for i, item in pairs(Character:GetChildren) do
    if item:IsA("Accessory") or item:IsA("Hat") then --Hats are old versions of accessories
        --Do your list thing here, item refers to the accessory on the character.
    end
end

I noticed that you’re trying to remove the accessory in the client-side, instead use a remote event and listen for accessory removal requests in the server:

AccessoryRemovalRemote.OnServerEvent:Connect(function(sender, accessory: Accessory)
   if accessory:IsDescendantOf(sender.Character) and accessory:IsA("Accessory") then -- make sure its their accessory
      accessory:Destroy()
   end
end)

Hey, i think that i found a better way instead:

task.wait(4)

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

local function updateAccessoryButtons()
	-- Clear the frame
	for _, child in ipairs(script.Parent.Parent.Frame.ScrollingFrame:GetChildren()) do
		if child:IsA('ImageButton') then
			child:Destroy()
		end
	end

	-- Add a button for each accessory
	for _, accessory in ipairs(character:GetChildren()) do
		if accessory:IsA('Accessory') then
			local button = Instance.new('ImageButton')
			button.Image = accessory.Handle.TextureID
			button.Parent = script.Parent.Parent.Frame.ScrollingFrame
			button.MouseButton1Click:Connect(function()
				accessory:Destroy()
			end)
		end
	end
end

-- Update the buttons whenever the character's accessories change
character.ChildAdded:Connect(updateAccessoryButtons)
character.ChildRemoved:Connect(updateAccessoryButtons)

-- Initial update
updateAccessoryButtons()

Except now the problem is the thumbnails because they display the texture of the accessory, there is a possibility that it would display a mesh of the accessory but i dont want that either, is there any way that i could get the thumbnails of the finished accessories in here?