Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Math.random pick more than 1 item?

Asked by 7 years ago
Edited 7 years ago

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

0
or, if someone has a better idea for a twinkle effect. MisterThackeryBinx 29 — 7y

3 answers

Log in to vote
2
Answered by 7 years ago
Edited 7 years ago

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.)

Ad
Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

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])

Log in to vote
0
Answered by 7 years ago

You could put it in a repeat loop

Answer this question