Randomized NPC Outfits Have Invisible Clothes

I’ve seen a topic on this before but no definitive answer was given. Hopefully this time around there will be one. The code below is split from two separate sections as to only show important parts:

local shirts = {
		7809811808,
		4383637715,
		7446653134,
		7569355578,
		7141216444,
	}
	
	local pants = {
		6816584241,
		6937192922,
		129459077,
		8016062227
	}

local randomshirtnum = math.random(1, #shirts)
local randompantsnum = math.random(1, #pants)

local randomshirt = shirts[randomshirtnum]
local randompants = pants[randompantsnum]
local npcshirt = clonenpc.Shirt
local npcpants = clonenpc.Pants

npcshirt.ShirtTemplate = "http://www.roblox.com/asset/?id="..randomshirt
npcpants.PantsTemplate = "http://www.roblox.com/asset/?id="..randompants

The method I’m currently using involves cloning a base NPC that already has a Shirt and Pants entity in them. Like I said I’ve seen a different thread revolving around this exact same problem but with no good solution. Thanks in advance for any help!

I would highly recommend using HumanoidDescriptions rather than cloning clothing objects.

For example, you can use the following code for the purpose stated in this thread:

local shirts = {
	7809811808,
	4383637715,
	7446653134,
	7569355578,
	7141216444,
}

local pants = {
	6816584241,
	6937192922,
	129459077,
	8016062227
}

local npc = "npc object"
local humanoid = npc:FindFirstChildOfClass("Humanoid")

if (npc:FindFirstChildOfClass("Shirt")) then
	npc:FindFirstChildOfClass("Shirt"):Destroy()
end
if (npc:FindFirstChildOfClass("Pants")) then
	npc:FindFirstChildOfClass("Pants"):Destroy()
end

local description = humanoid:GetAppliedDescription()
description.Shirt = shirts[math.random(1, #shirts)]
description.Pants = pants[math.random(1, #pants)]

humanoid:ApplyDescription(description)

If you found this useful, please mark it as a solution so others can find the answer right away. If you have any more questions, feel free to reply and I will answer them for you.

This worked perfectly, thank you! Only thing is that the skin color for all the NPC is turned black, but, if I remember correctly, that is just another thing that needs to be changed in the humanoid descriptor. Thanks!

Yes, you just need to change the skin colors via the HumanoidDescription. You can either:

  1. Edit the skin colors via the description variable in the code
    or
  2. Add a HumanoidDescription in the NPC, and since the code calls the NPC’s existing description, it will be kept when the clothing is updated.