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

how does math.random work?

Asked by
iNicklas 215 Moderation Voter
8 years ago

I don't really understand it.

If i have 9 bricks inside a model, one of the bricks gets randomly picked to be transparency = 1 and cancollide = false

How would i do that?

All bricks are named Part, but all have different colors.

red black yellow cyan blue purple pink white green

local model = script.Parent

while true do
    wait(5)

    math.random(1,9)

end
0
Can you ever look at a wiki article? unmiss 337 — 8y

3 answers

Log in to vote
2
Answered by 8 years ago

What I would do is use a table. I'll explain in the code.

local model = script.Parent --Create a variable to easily go to script.Parent

while true do --Create an infinite loop
    wait(5) --wait 5 seconds
    local parts = {} --Create an empty table called parts and keep it local to this loop
    for i,v in pairs(model:GetChildren()) do --Search the entire model
        if v:IsA("BasePart") then --if the object found in the model is a part then...
            table.insert(parts, #parts+1, v) --insert the part's data into the "parts" table
        end --tell the script that that is the end of the if statement
    end --tell the script that that is the end of the search
    local randompart = parts[math.random(1, #parts)] --create a new variable and set it to a value in the parts table.
    --Now edit the randomly selected part here
end --tell the script that that is the end of the loop

--[[
    How this works is when you use the math.random() it selects a random number from the first 
    argument to the second argument. If you have worked with tables before you may remember
    that you can select a value in the table using this: Table[1] What Table[1] does is it returns the
    first value in that table. If you need to get the number of values in a table then you would use 
    #Table. That returns the amount of values in that table. 
    So when you do parts[math.random(1, #parts)] you are asking the script to go to the parts,
    then you ask math.random to return a random number from 1 to however many values are in
    the parts table. Ergo, creating a way to find a random part. There is a much simpler way, but
    it is rather irritating to work with. If you want the simpler way let me know!
]]--
Ad
Log in to vote
1
Answered by 8 years ago

I know this is already answered but this is so you could understand this better.


math.random() makes a random number from 0 to 1

print(math.random()) --> 0.174852667328662

math.random(100) with only 1 number in it will choose a number from 1 to 100.

math.random(100) --> 74

math.random(1,100) is the same thing.


For making random brick color. Most people assume that you make an if then statement and if math.random() is a specific number the part is a random color. That is not the case, you can do this:

workspace.Part.BrickColor = BrickColor.Random()

Use BrickColor.Random()



Hope it helps!

Log in to vote
0
Answered by 8 years ago

It is very simple to get a random Instance from another Instance. You can get an array of all children like such:

local parts = model:GetChildren()

You can then use math.random()to get a random instance (or part) from here. We use # to count the number of entries in the array, and [number] to get an entry. if math.random(9) returns '5' we will return entry number 5.

parts[math.random(#parts)]

Do note that this will return any child, for example the very script. Make sure only the nine Parts are in that Instance/Model you are reading from.

Answer this question