Pick random object from folder

Code:

local module = {}
function module:init()
	createNPC()
	createNPC()
	createNPC()
	createNPC()
	createNPC()
	createNPC()
	createNPC()
	createNPC()
end
function createNPC()
	pcall(function()
		local npc = script.Storage.NPC:Clone()
		local hairs = script.Storage.Hair:GetChildren()
		local faces = script.Storage.Face:GetChildren()
		local shirts = script.Storage.Shirt:GetChildren()
		local pants = script.Storage.Pants:GetChildren()
		local hair = math.random(1, #hairs)
		local face = math.random(1, #faces)
		local shirt = math.random(1, #shirts)
		local pant = math.random(1, #pants)
		hair:Clone()
		face:Clone()
		shirt:Clone()
		pant:Clone()
		shirt.Name = "Shirt"
		pant.Name = "Pants
		hair.Parent = npc
		face.Parent = npc.Head
		shirt.Parent = npc 
		pant.Parent = npc 
		npc.Parent = game.Workspace
		local spawnLocation = script.Storage.Spawns:GetChildren()[math.random(#script.Storage.Spawns:GetChildren())]:Clone()
		npc:SetPrimaryPartCFrame(spawnLocation.CFrame * CFrame.Angles(0,math.rad(math.random(360)),0) * CFrame.new(0,3,0))
	end)
end
return module

Error:
image

I don’t know the problem with it.

Try this maybe:

local module = {}
function module:init()
	createNPC()
	createNPC()
	createNPC()
	createNPC()
	createNPC()
	createNPC()
	createNPC()
	createNPC()
end
function createNPC()
	pcall(function()
		local npc = script.Storage.NPC:Clone()
		local hairs = script.Storage.Hair:GetChildren()
		local faces = script.Storage.Face:GetChildren()
		local shirts = script.Storage.Shirt:GetChildren()
		local pants = script.Storage.Pants:GetChildren()
		local hair = hairs[math.random(1, #hairs)]
		local face = faces[math.random(1, #faces)]
		local shirt = shirts[math.random(1, #shirts)]
		local pant = pants[math.random(1, #pants)]
		hair:Clone()
		face:Clone()
		shirt:Clone()
		pant:Clone()
		shirt.Name = "Shirt"
		pant.Name = "Pants
		hair.Parent = npc
		face.Parent = npc.Head
		shirt.Parent = npc 
		pant.Parent = npc 
		npc.Parent = game.Workspace
		local spawnLocation = script.Storage.Spawns:GetChildren()[math.random(#script.Storage.Spawns:GetChildren())]:Clone()
		npc:SetPrimaryPartCFrame(spawnLocation.CFrame * CFrame.Angles(0,math.rad(math.random(360)),0) * CFrame.new(0,3,0))
	end)
end
return module
local module = {}
function module:init()
	createNPC()
	createNPC()
	createNPC()
	createNPC()
	createNPC()
	createNPC()
	createNPC()
	createNPC()
end
function createNPC()
	pcall(function()
		local npc = script.Storage.NPC:Clone()
		local hairs = script.Storage.Hair:GetChildren()
		local faces = script.Storage.Face:GetChildren()
		local shirts = script.Storage.Shirt:GetChildren()
		local pants = script.Storage.Pants:GetChildren()
		local hair = hairs[math.random(1, #hairs)]
		local face = faces[math.random(1, #faces)]
		local shirt = shirts[math.random(1, #shirts)]
		local pant = pants[math.random(1, #pants)]
		hair:Clone()
		face:Clone()
		shirt:Clone()
		pant:Clone()
		shirt.Name = "Shirt"
		pant.Name = "Pants"
		hair.Parent = npc
		face.Parent = npc.Head
		shirt.Parent = npc 
		pant.Parent = npc 
		npc.Parent = game.Workspace
		local spawnLocation = script.Storage.Spawns:GetChildren()[math.random(#script.Storage.Spawns:GetChildren())]:Clone()
		npc:SetPrimaryPartCFrame(spawnLocation.CFrame * CFrame.Angles(0,math.rad(math.random(360)),0) * CFrame.new(0,3,0))
	end)
end
return module

you made a typo

1 Like

It works but only 3 spawn. I don’t know the problem.