Problem randomize NPC outfits

I am trying randomize the outfits of NPC’s that spawn with a list of shirts and pants. I’m having an issue when the NPC spawns they spawn with the shirt and pants with the intended ID’s but the clothes don’t show. They’re parented in the NPC but they aren’t appearing on said NPC. {The script doesnt throw any errors}

Is there anything I am doing wrong or forgeting?

local Shirts = {"rbxassetid://5258364225", "rbxassetid://4772210886", "rbxassetid://6714294027", "rbxassetid://7239470863", "rbxassetid://5092216954", 
	"rbxassetid://7730388262", "rbxassetid://5473281165", "rbxassetid://6671990960", "rbxassetid://7144612977", "rbxassetid://4955872544", "rbxassetid://1138763401", 
	"rbxassetid://5238572799", "rbxassetid://7250317770",}

local Pants = {"rbxassetid://6560857596", "rbxassetid://1710704401", "rbxassetid://5258365040", "rbxassetid://7250317770", "rbxassetid://4801770796"}

	local RandomShirt = Shirts[math.random(1,#Shirts)]
				local RandomPants = Pants[math.random(1,#Pants)]
				local RandomFace = Clothing.Faces:GetChildren()[math.random(1,#Clothing.Faces:GetChildren())]
				
				NPC:FindFirstChild("Shirt"):Destroy()
				NPC:FindFirstChild("Pants"):Destroy()				
				
				local shirt = Instance.new("Shirt",NPC)
				shirt.Name = ("Shirt")
				shirt.ShirtTemplate = RandomShirt
				
				local pants = Instance.new("Pants", NPC)
				pants.Name = ("Pants")
				pants.PantsTemplate = RandomPants
				
				local face = RandomFace:Clone()
				face.Face = ("Front")
				


shirt.Parent = NPC
				pants.Parent = NPC
				face.Parent = NPC.Head
				
			end
			
			repeat
				spon = Spawns:GetChildren()[math.random(1,#Spawns:GetChildren())]
				wait(.5)
			until spon:IsA("BasePart")
			
			local clone = NPC:Clone()
			
			clone.Parent = NPCs			
			clone:MoveTo(Vector3.new(spon.Position.X, spon.Position.Y + 5, spon.Position.Z))
			clone.lvl.Value = LevelRange
			local Formula = math.floor(100+ (clone.lvl.Value*2))
			clone.Humanoid.MaxHealth = Formula
			clone.Humanoid.Health = Formula
			
			clone.Name = ("lvl: "..clone.lvl.Value.." | "..clone.Name.." | ⚕️ "..clone.Humanoid.MaxHealth)
			

	
				

The NPC is created after you try to put in the shirt and pants, that’s why it doesn’t appear.

The script always works from the top to the bottom, always in that order.

Fixed it for you and removed some unnecessary code.

local Shirts = {"rbxassetid://5258364225", "rbxassetid://4772210886", "rbxassetid://6714294027", "rbxassetid://7239470863", "rbxassetid://5092216954", 
	"rbxassetid://7730388262", "rbxassetid://5473281165", "rbxassetid://6671990960", "rbxassetid://7144612977", "rbxassetid://4955872544", "rbxassetid://1138763401", 
	"rbxassetid://5238572799", "rbxassetid://7250317770",}

local Pants = {"rbxassetid://6560857596", "rbxassetid://1710704401", "rbxassetid://5258365040", "rbxassetid://7250317770", "rbxassetid://4801770796"}

	local RandomShirt = Shirts[math.random(1,#Shirts)]
				local RandomPants = Pants[math.random(1,#Pants)]
				local RandomFace = Clothing.Faces:GetChildren()[math.random(1,#Clothing.Faces:GetChildren())]
				
				
repeat
				spon = Spawns:GetChildren()[math.random(1,#Spawns:GetChildren())]
				wait(.5)
			until spon:IsA("BasePart")
			
			local clone = NPC:Clone()
			
			clone.Parent = NPCs			
			clone:MoveTo(Vector3.new(spon.Position.X, spon.Position.Y + 5, spon.Position.Z))
			clone.lvl.Value = LevelRange
			local Formula = math.floor(100+ (clone.lvl.Value*2))
			clone.Humanoid.MaxHealth = Formula
			clone.Humanoid.Health = Formula
			
			clone.Name = ("lvl: "..clone.lvl.Value.." | "..clone.Name.." | ⚕️ "..clone.Humanoid.MaxHealth)
			
                                			
				
				local shirt = Instance.new("Shirt", clone)
				shirt.Name = ("Shirt")
				shirt.ShirtTemplate = RandomShirt
				
				local pants = Instance.new("Pants", clone)
				pants.Name = ("Pants")
				pants.PantsTemplate = RandomPants
				
				local face = RandomFace:Clone()
				face.Face = ("Front")
				face.Parent = clone.Head


				
			end
			

The same problem is still happening. Still Nude.

17:24:36.751 Image https://assetdelivery.roblox.com/v1/asset?id=5092216954 failed to load. Error 403: Asset type does not match requested type -

Hey, I took a look at the problem and I found that attempting to set Shirt, and Pants template does nothing, I don’t know what’s causing this issue, however, I resolved it by using HumanidDescription instance. I also made few changes to your code to make it more clear and efficient.

local Workspace = game:GetService("Workspace")

local Catalog = {
	shirts = {
		5258364225,
		4772210886,
		6714294027,
		7239470863,
		5092216954,
		7730388262,
		5473281165,
		6671990960,
		7144612977,
		4955872544,
		1138763401,
		5238572799,
		7250317770
	},
	pants = {
		6560857596,
		1710704401,
		5258365040,
		7250317770,
		4801770796
	},
	faces = {
		25166274,
		18151826
	}
}

local function RandomizeAppearance(npc)
	-- Get random ids
	local randomShirtId = Catalog.shirts[math.random(1, #Catalog.shirts)]
	local randomPantsId = Catalog.pants[math.random(1, #Catalog.pants)]
	local randomFaceId = Catalog.faces[math.random(1, #Catalog.faces)]
	
	local skinColor = Color3.fromRGB(204, 142, 105)

	local humanoidDescription = Instance.new("HumanoidDescription")
	
	-- Apply premade skin color
	humanoidDescription.HeadColor = skinColor
	humanoidDescription.LeftArmColor = skinColor
	humanoidDescription.RightArmColor = skinColor
	humanoidDescription.LeftLegColor = skinColor
	humanoidDescription.RightLegColor = skinColor
	humanoidDescription.TorsoColor = skinColor
	
	-- Apply random clothing and a face
	humanoidDescription.Shirt = randomShirtId
	humanoidDescription.Pants = randomPantsId
	humanoidDescription.Face = randomFaceId
	
	-- Apply the appearance to the humanoid
	npc.Humanoid:ApplyDescription(humanoidDescription)
	
	-- Discard the instance as we will longer need it
	humanoidDescription:Destroy()
end

RandomizeAppearance(Workspace.Dummy)

Also for your future scripts, avoid the same prefix for your items inside of a table as its less readable and takes more space, both in editor and the memory. It also removes the problem where you need to update all of the item’s prefixes.

Bad.

local shirts = {"rbxassetid://5258364225", "rbxassetid://4772210886"}
local shirtURL = shirts[1, #shirts]

Good.

local shirts = {5258364225, 4772210886}
local shirtURL = "rbxassetid://".. shirts[1, #shirts]

It’s also a good practice to store the ids in a module and require it in the script, this will reduce the clutter in your code.

module.lua

return {
	shirts = {5258364225},
	pants = {6560857596},
	faces = {25166274}
}

main.Lua

local Catalog = require(script.Catalog)

Also if you didn’t know already you can replace NumberValue or IntValue with attributes.

Assigning value to an attribute

NPC:SetAttribute("lvl", 1) -- clone.lvl.Value = 1

Getting value from an attribute

NPC:GetAttribute("lvl") -- clone.lvl.Value

And also keep in mind the code I provided is not the same so you will have to change it up to meet your needs.

2 Likes

I know this post is old but I just wanted to ask if the “rbxassetid://” is really needed? From my experience all it takes is just putting in the ID and the asset still loads.
And unfortunately, changing the HumanoidDescription’s shirt and pants doesn’t do much

Some properties auto format the ID into correct URL, but some don’t. Generally, I would recommend to prefix with rbxassetid:// when you are referring to an asset by its ID. As for HumanoidDescription, I don’t know.

1 Like