Randomly "choose" multiple parts from a folder of parts

  1. What do you want to achieve? Keep it simple and clear!
    Randomly “choose” multiple parts from a folder of parts
  2. What is the issue? Include screenshots / videos if possible!
    I could do a if, (random number) then “select part” script but I have 110 possible parts to choose from.
    I haven’t found a way to use math random #items to select more than one part. I want to replicate the chosen parts to workspace
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I saw a few similar posts however It was requesting one item, I want to be able to select about 20-30 parts at a time and replicate them in pairs.

I know this has certainly been posted before and I really have tried to look. I apologize if I upset you for asking something that has already answered.

Pseudo code to give you an idea:

local totalPossibleParts {}
local selectedPartsINDEX = {}
local selectedParts = {}
--Step 1:
--inserts every part's name into totalPossibleParts, basically turning the hierarchy into a table 
--for loop here, loops through the whole folder, table.insert(totalPossibleParts, v.Name) 

--Step 2:
--start choosing the randoms
local numberOfPartsToChoose = 3 --your number here
for i=1, numberOfPartsToChoose do
   local generateRandom = math.random(1, #totalPossibleParts)
   table.insert(selectedPartsINDEX, generateRandom)
end

--Step 3:
--convert the Chosen Indexes into the parts name to return in selectedParts
for _, v in pairs (selectedPartsINDEX) do 
   local partIndex = tonumber(v)
   table.insert(selectedParts, tostring(totalPossibleParts[partIndex]))
end

By the way, this is all pseudo code so, you’ll have to change some of these elements up. This code is just the logic/idea.

1 Like

Thank you so much!! this is exactly what I was looking for. I appreciate the time you took.
Would getchildren() work for the total possible parts?

Oops! Yes, you’ll definitely need that:

local totalPossibleParts = yourFolder:GetChildren()

hope this helps!

1 Like

I’m coming across an issue, Sometimes it successfully returns all 20, and sometimes it returns nil. I added to the code to remove after selection so the same part doesn’t get chosen twice, and then to replicate that part to a folder in workspace.
index
chosenparts
Here’s my code

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local totalPossibleParts = game.ReplicatedStorage.proxinstances:GetChildren()
local selectedPartsINDEX = {}
local selectedParts = {}
--Step 1:
--inserts every part's name into totalPossibleParts, basically turning the hierarchy into a table 
--for loop here, loops through the whole folder, table.insert(totalPossibleParts, v.Name) 

--Step 2:
--start choosing the randoms
local numberOfPartsToChoose = 20 --your number here
for i=1, numberOfPartsToChoose do
	local generateRandom = math.random(1, #totalPossibleParts)
	table.remove(totalPossibleParts, generateRandom)
	table.insert(selectedPartsINDEX, generateRandom)
end

for _, v in pairs (selectedPartsINDEX) do 
	local partIndex = tonumber(v)
	table.insert(selectedParts, tostring(totalPossibleParts[partIndex]))
end

print(selectedParts)

for num, part in pairs(selectedParts) do
	p2bclone = game.ReplicatedStorage.proxinstances:FindFirstChild(part):Clone()
	p2bclone.Parent = game.Workspace.ChosenParts
end

Let me know if you have any ideas! thanks again

1 Like

excuse me, but I think I might as well made a little mistake on the math.random (sorry! but we all make mistakes…) instead of math.random(1, #totalPossibleParts) change the “1” to “0”.

It’s choosing number 2 or above, and it skips 1.
Now, after the fix, it should work properly (chooses number 1 or above).

edit: if there is still an issue, you might have to do some debugging. I’m guessing when you got nil, it could also be because #totalPossibleParts is actually (#totalPossibleParts + 1) so you might have to subtract 1 to see if it works (#totalPossibleParts - 1).

1 Like

Awesome thank you :slight_smile: this is integral for my game to work so i’m super appreciative. have a good day

1 Like

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