What do you want to achieve? Keep it simple and clear!
Randomly “choose” multiple parts from a folder of parts
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
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.
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.
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.
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
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).