Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Math.random pick more than 1 item?

Hello,

Using math.random, how would I use that to pick more than 1 brick inside of a model. I know how to pick one. But, again, how would I use math.random to pick two or more bricks.

bricks = game.workspace.Bricks:GetChildren()
math.random(1,#bricks)


Thanks, Antux

1 comment

0

or, if someone has a better idea for a twinkle effect.

3 answers

whenallthepigsfly's answer is good, but has the flaw that you might get duplicate bricks (math.random might return the same number). If that's not okay in whatever you're doing, you should delete the brick from the table:

local bricks = game.workspace.Bricks:GetChildren() 
local bricksChosen = {}
local numberOfBricksWanted = 2

for i = 1, numberOfBricksWanted do
	if #bricks == 0 then break end
	local index = math.random(1, #bricks)
	bricksChosen[i] = bricks[i]
	table.remove(bricks, i)
end

(Script is modified from whenallthepigsfly's answer. Note that in whenallthepigsfly's script, 'bricksChosen' is a table of integers, whereas mine is a table of the chosen bricks.)

Repeat it more than once, in a for loop.

local bricks = game.workspace.Bricks:GetChildren() 
local bricksChosen = {}
local numberOfBricksWanted = 2

for i = 1, numberOfBricksWanted do
     bricksChosen[i] = math.random(1,#bricks)
end

It will repeat the search for the given amount, and put the chosen bricks into an array you can later read from using the same notation (Array[place])

This discussion is preserved as it was. New answers, comments, and votes are closed.