NPC spawning with random clothes not working

I want to make NPCs spawn in with different sets of clothes and have different genders. For some reason it is not working though. The script is parented to an NPC.

local s = script.Parent:FindFirstChildWhichIsA("Shirt")
local p = script.Parent:FindFirstChildOfClass("Pants")

local picker1 = math.random(1, 4)

if picker1 == 1 then -- blue suit female
	s.ShirtTemplate = "http://www.roblox.com/asset/?id=6271127911"
	p.PantsTemplate = "http://www.roblox.com/asset/?id=6279687903"
	s.Color3 = Color3.new(255, 255, 255)
	p.Color3 = Color3.new(255, 255, 255)
	local femtorso = script.CharacterMesh:Clone()
	femtorso.Parent = script.Parent
elseif picker1 == 2 then -- blue suit male
	s.ShirtTemplate = "http://www.roblox.com/asset/?id=6271127911"
	p.PantsTemplate = "http://www.roblox.com/asset/?id=6279687903"
	s.Color3 = Color3.new(255, 255, 255)
	p.Color3 = Color3.new(255, 255, 255)
elseif picker1 == 3 then -- green suit female
	s.ShirtTemplate = "http://www.roblox.com/asset/?id=18883135239"
	p.PantsTemplate = "http://www.roblox.com/asset/?id=18883142240"
	s.Color3 = Color3.new(255, 255, 255)
	p.Color3 = Color3.new(255, 255, 255)
	local femtorso = script.CharacterMesh:Clone()
	femtorso.Parent = script.Parent
elseif picker1 == 4 then -- green suit male
	s.ShirtTemplate = "http://www.roblox.com/asset/?id=18883135239"
	p.PantsTemplate = "http://www.roblox.com/asset/?id=18883142240"
	s.Color3 = Color3.new(255, 255, 255)
	p.Color3 = Color3.new(255, 255, 255)
end

What’s interesting is that the NPC will spawn with either male torso or female torso, so that part works. It’s just the clothes that don’t work.

Help would be much appreciated!

There’s an easier way to accomplish this, just store all the outfits inside a table then iterate through it using math.random


local outfits = {

-- Outfits 

}

local randomPick = math.random(1, #outfits)
local chosenOutfit = outfits[randomPick]

I’ve remade the script like this:

local s = script.Parent:FindFirstChild("Shirt")
local p = script.Parent:FindFirstChild("Pants")

local outfits = {"Blue Suit (Female)",
	"Blue Suit (Male)",
	"Green Suit (Female)",
	"Green Suit (Male)"
}
print("randomPick firing...")
local randomPick = math.random(1, #outfits)
print("Number of outfits is", #outfits)
print("randomPick finished...")
print(randomPick)
local chosenOutfit = outfits[randomPick]
print(chosenOutfit)

if chosenOutfit == "Blue Suit (Female)" then -- blue suit female
	print("If chosenOutfit == 'Blue Suit (Female)' then")
	s.ShirtTemplate = "http://www.roblox.com/asset/?id=6271127911"
	p.PantsTemplate = "http://www.roblox.com/asset/?id=6279687903"
	s.Color3 = Color3.new(255, 255, 255)
	p.Color3 = Color3.new(255, 255, 255)
	local femtorso = script.CharacterMesh:Clone()
	femtorso.Parent = script.Parent
	print("'If Blue Suit (Female)' section finished!")
	
elseif chosenOutfit == "Blue Suit (Male)" then -- blue suit male
	print("If chosenOutfit == 'Blue Suit (Male)' then")
	s.ShirtTemplate = "http://www.roblox.com/asset/?id=6271127911"
	p.PantsTemplate = "http://www.roblox.com/asset/?id=6279687903"
	s.Color3 = Color3.new(255, 255, 255)
	p.Color3 = Color3.new(255, 255, 255)
	print("'If Blue Suit (Male)' section finished!")
	
elseif chosenOutfit == "Green Suit (Female)" then -- green suit female
	print("If chosenOutfit == 'Green Suit (Female)' then")
	s.ShirtTemplate = "http://www.roblox.com/asset/?id=18883135239"
	p.PantsTemplate = "http://www.roblox.com/asset/?id=18883142240"
	s.Color3 = Color3.new(255, 255, 255)
	p.Color3 = Color3.new(255, 255, 255)
	local femtorso = script.CharacterMesh:Clone()
	femtorso.Parent = script.Parent
	print("'If Green Suit (Female)' section finished!")
	
elseif chosenOutfit == "Green Suit (Male)" then -- green suit male
	print("If chosenOutfit == 'Green Suit (Male)' then")
	s.ShirtTemplate = "http://www.roblox.com/asset/?id=18883135239"
	p.PantsTemplate = "http://www.roblox.com/asset/?id=18883142240"
	s.Color3 = Color3.new(255, 255, 255)
	p.Color3 = Color3.new(255, 255, 255)
	print("'If Green Suit (Male)' section finished!")
end

I’ve added a load of prints to make sure everything in the script is actually firing. And I’ve verified that this is true. I think the issue is purely a matter of the shirts/pants not loading/showing up for some reason.

After further research I found out I needed to use HumanoidDescription to fix this issue. Here’s my fixed script!

local humanoidDescription = Instance.new("HumanoidDescription")

local outfits = {
	"Blue Suit (Female)",
	"Blue Suit (Male)",
	"Green Suit (Female)",
	"Green Suit (Male)"
}

local skincolors = {
	"Black",
	"Tan",
	"White"
}

local blackcolor = Color3.fromRGB(90, 76, 66)
local tancolor = Color3.fromRGB(204, 142, 105)
local whitecolor = Color3.fromRGB(234, 184, 146)

print("randomClothes firing...")
local randomClothes = math.random(1, #outfits)
print("Number of outfits is", #outfits)
print("randomClothes finished...")
print(randomClothes)
local chosenOutfit = outfits[randomClothes]
print(chosenOutfit)

if chosenOutfit == "Blue Suit (Female)" then -- blue suit female
	print("If chosenOutfit == 'Blue Suit (Female)' then")
	humanoidDescription.Shirt = 6271127911
	humanoidDescription.Pants = 6279687903
	humanoidDescription.Torso = 48474356
	print("'If Blue Suit (Female)' section finished!")
	
elseif chosenOutfit == "Blue Suit (Male)" then -- blue suit male
	print("If chosenOutfit == 'Blue Suit (Male)' then")
	humanoidDescription.Shirt = 6271127911
	humanoidDescription.Pants = 6279687903
	print("'If Blue Suit (Male)' section finished!")
	
elseif chosenOutfit == "Green Suit (Female)" then -- green suit female
	print("If chosenOutfit == 'Green Suit (Female)' then")
	humanoidDescription.Shirt = 18883135239
	humanoidDescription.Pants = 18883142240
	humanoidDescription.Torso = 48474356
	print("'If Green Suit (Female)' section finished!")
	
elseif chosenOutfit == "Green Suit (Male)" then -- green suit male
	print("If chosenOutfit == 'Green Suit (Male)' then")
	humanoidDescription.Shirt = 18883135239
	humanoidDescription.Pants = 18883142240
	print("'If Green Suit (Male)' section finished!")
end

local randomSkin = math.random(1, #skincolors)
local chosenSkin =  skincolors[randomSkin]

if chosenSkin == "Black" then
	humanoidDescription.HeadColor = blackcolor
	humanoidDescription.TorsoColor = blackcolor
	humanoidDescription.LeftArmColor = blackcolor
	humanoidDescription.RightArmColor = blackcolor
	humanoidDescription.LeftLegColor = blackcolor
	humanoidDescription.RightLegColor = blackcolor
elseif chosenSkin == "Tan" then
	humanoidDescription.HeadColor = tancolor
	humanoidDescription.TorsoColor = tancolor
	humanoidDescription.LeftArmColor = tancolor
	humanoidDescription.RightArmColor = tancolor
	humanoidDescription.LeftLegColor = tancolor
	humanoidDescription.RightLegColor = tancolor
elseif chosenSkin == "White" then
	humanoidDescription.HeadColor = whitecolor
	humanoidDescription.TorsoColor = whitecolor
	humanoidDescription.LeftArmColor = whitecolor
	humanoidDescription.RightArmColor = whitecolor
	humanoidDescription.LeftLegColor = whitecolor
	humanoidDescription.RightLegColor = whitecolor
end

script.Parent.Humanoid:ApplyDescription(humanoidDescription)

humanoidDescription:Destroy()

Planning to add more things like randomized hair.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.