For my game, I have NPCs of developers in the team, and I wanted to automatically change the NPC look to match the developer’s new look should they update their avatar at any point in time.
Issue
When calling the script provided below, some accessories DO get removed while other accessories stay (this isn’t just for accessories, as Shirt and Pants have the same issue)
not entirely sure what’s going on here but…
I also tried to make my own function to clear the character’s appearance as well though it’s still running into issues where the NPCs of my character work fine, while everyone else’s NPCs have no accessories or anything applied to them.
Note
The NPCs that are in the game are originally fetched with AlreadyPro’s plugin
local Players = game:GetService("Players")
local NPCHolder = SomeModel -- Model filled with NPCs here
function ClearAppearance(NPC)
for _, Obj in ipairs(NPC:GetChildren()) do
if Obj:IsA("Shirt") or Obj:IsA("Pants") or Obj:IsA("Accessory") then
Obj:Destroy()
end
end
end
function UpdateCharacters()
for _, NPC in pairs(NPCHolder:GetChildren()) do
local UserSuccess, UserID = pcall(function()
return Players:GetUserIdFromNameAsync(NPC.Name)
end)
if UserSuccess then
local DescSuccess, HumanoidDesc = pcall(function()
return Players:GetHumanoidDescriptionFromUserId(UserID)
end)
if DescSuccess then
ClearAppearance(NPC)
NPC.Humanoid:ApplyDescription(HumanoidDesc)
end
end
end
end
UpdateCharacters()
while wait(32) do
UpdateCharacters()
end
After further testing, with @Grargror 's help, I found what’s happening here:
Since I initially added the NPC’s with the plugin, the accessories were already on the character and they matched with the HumanoidDescription Properties, HOWEVER… Since it was added in via plugin the Description itself was never set to the NPC’s Humanoid, so the existing accessories are there without the humanoid being aware of them.
This was pretty tricky to find because some accessories were actually being removed (like my back accessories and such) but others weren’t…
I’m not sure if this should remain in this category (if it’s a bug or not) but the Humanoid should definitely clear out everything no matter if the Description has been set or not
I am in the process of checking over bug reports and following up on some bugs that haven’t received any activity in a while.
Is this issue still occurring or can you confirm that this bug has been resolved?
Hey @Dev0mar how did you end up fixing this? I have the same issue.
Edit: using the ClearAppearance code from above worked in my case. One extra gotcha to to make sure the LoadAppearance call succeeds before clearing the appearance. It seems to fail a small percentage of the time, leaving you with a naked guy.
function ClearAppearance(NPC)
for _, Obj in ipairs(NPC:GetChildren()) do
if Obj:IsA("Shirt") or Obj:IsA("Pants") or Obj:IsA("Accessory") then
Obj:Destroy()
end
end
end
Would still like to find a better solution that is more future-proof to Roblox adding new item types like Shirts and Pants.