How to remove accessories by script?

Hello! I made an event script that changes appearance of npc by username. This is how it looks like:

local event = game:GetService("ReplicatedStorage").RemoteEvent2
local dummy = script.Parent

event.OnServerEvent:Connect(function(player, aa)
	local userid = game:GetService("Players"):GetUserIdFromNameAsync(aa)
	local info = game:GetService("Players"):GetCharacterAppearanceInfoAsync(userid)

local Dummy = game:GetService("Workspace").Dummy
local username = aa

local bodicolorz = game:GetService("Workspace").Dummy["Body Colors"]

bodicolorz.LeftArmColor = BrickColor.new(info["bodyColors"]["leftArmColorId"])
bodicolorz.RightArmColor = BrickColor.new(info["bodyColors"]["rightArmColorId"])
bodicolorz.LeftLegColor = BrickColor.new(info["bodyColors"]["leftLegColorId"])
bodicolorz.RightLegColor = BrickColor.new(info["bodyColors"]["rightLegColorId"])
bodicolorz.TorsoColor = BrickColor.new(info["bodyColors"]["torsoColorId"])
bodicolorz.HeadColor = BrickColor.new(info["bodyColors"]["headColorId"])

local insertservice = game:GetService("InsertService")

for i = 1,#info["assets"] do
	local aaa = insertservice:LoadAsset(info["assets"][i]["id"])
	aaa.Parent = Dummy
	aaa:GetChildren()[1].Parent = Dummy
	aaa:Destroy()
end
end)

But if the client wants to change the npc to another, then on npc will be old accessories. I didn’t find anything about that, so I’m asking for help.

Have you tried using the humanoid description system? It is more simple and straight forward.
Your code could look like this.

event.OnServerEvent:Connect(function(player, aa)
	local userid = game:GetService("Players"):GetUserIdFromNameAsync(aa)
	local description = game:GetService("Players"):GetHumanoidDescriptionFromUserId(userid)

local Dummy = game:GetService("Workspace").Dummy
local DummyHumanoid = Dummy.Humanoid
local username == aa

DummyHumanoid:ApplyDescription(description)
end
end)

This should work as long as the dummy has a humanoid.

1 Like

You can learn about Humanoid:ApplyDescription here: Humanoid | Documentation - Roblox Creator Hub

1 Like

Damn! I have never seen this before, but thank you so much, you helped me so much!

1 Like

You bet! I’ve heard the GetDescription and ApplyDescription functions can fail occasionally. I’ve never really had a problem with it but if you end up having problems with it, it would be a good idea to wrap them in pcalls.

1 Like