What would be a way to create a table of children of a model, and then clone a random one of them?

I’m pretty new to tables (and humanoids, but that’s off-topic), and I was wondering if there is a way for me to create a table using the children of a folder or model, then select one of them and clone it.

Right now I used this script:

local Merch = script.Parent.MerchSelection:GetChildren()
local PickOne = math.random(1,#Merch)
PickOne:Clone().Parent = script.Parent

Which didn’t work. The reason might be obvious to pros in this domain, but for me not really.

The only other way I can think of is to manually create a table with the IDs of the shifts inside, then randomly select one of them and paste it over an already existing empty shirt in the NPC, but I thought it would be better if I can put this question up, possibly get a more versatile and simple way of doing it, and in the process learn more about tables and how they function.

Thanks in advance for any help! c:

1 Like

This doesn’t work because you are picking a pseudo-random number between 1 and the length of the table. You need to access from the table:

- local PickOne = math.random(1,#Merch)
+ local PickOne = Merch[math.random(#Merch)]
4 Likes