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

I am trying to make a random color script, but only with a few select colors, am I doing this right?

Asked by 7 years ago

I am trying to make a random color script, but only with a few select colors, am I doing this right?

RandomNumber = math.random(1,10)
1 = 38
2 = 303
3 = 106
4 = 105
5 = 192
6 = 365
7 = 333
8 = 217
9 = 1017
10 = 18
script.Parent.BrickColor = RandomNumber

1 answer

Log in to vote
4
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
7 years ago
Edited 7 years ago

Answer

No, you did not make the script right.


Problem

You have several issues with your script.

  • You are expecting the script to set a BrickColor to a number value. BrickColors can not take these values and will error stating it was expecting a BrickColor value.
  • You are expecting the variable RandomNumber to be picked up as one of the ten other variables you set up. This is not the case. With your math.random function, the script is setting the variable RandomNumber to a number between 1 to 10. The script does not exactly know you're trying to use the variables 1 to 10.
  • You're trying to set numbers to numbers. Scripts won't be able to recognize that you're setting a number to a number. It will just think you're doing some bad math or something.

Solution

  • You would need to set a BrickColor property equal to a BrickColor value, so you will want to use the function BrickColor.new().
  • Make a table and add the numbers to it. Tables have index values starting at 1 to however many values there are in the table.
  • With the table, we will be able to index the numbers from 1 to 10. Because tables are set up in such a way that they have an index and an actual value. { index=1&value=38, index=2&value=303 } just try to imagine the table like that. You're not necessarily setting numbers to numbers in this table.

Recommendation

I wouldn't necessarily use numbers as they would become difficult to translate to BrickColors. If you do use numbers, just leave a comment for yourself to find out which color is which. That way if you want to add or remove a color, they'd be easy to find. Alternatively, you can use the color names in string form in the table.


Explanation

So, on line 1 we're defining the table and taking all the BrickColor values you wanted into it. On line 13, we're setting the script's parent line color to a new BrickColor value.

Line 13 may seem confusing. We're setting the BrickColor property to a BrickColor value with BrickColor.new(). Within BrickColor.new() we are using the variable for the table which is BrickColors. With the BrickColors table, we're using the brackets as a form of indexing the table. Since the table is in order in terms of indexing, we can use math.random() to get us a random number between 1 and however many entries are in the table. The only necessary argument is how many entries are in the table. We can easily indicate how many entries are in a table by just using the # operator.


Final Script

BrickColors = {
    38, --Dark orange
    303, --Dark blue
    106, --Bright orange
    105, --Br. yellowish orange
    192, --Reddish brown
    365, --Burnt Sienna
    333, --Gold
    217, --Brown
    1017, --Deep orange
    18, --Nougat
}
script.Parent.BrickColor = BrickColor.new(BrickColors[math.random(#BrickColors)])

Hopefully, this answered your question. If it did, do not forget to hit the accept answer button. If you have any questions, feel free to comment below.
Ad

Answer this question