Simple number rolling issue

numbers = {3,6,9,10}

num = numbers[math.random(1,#numbers)]

script.Parent.Value = num

I’m trying to make a script where when the number lands on 3, it will change the BrickColor of script.Parent to “Really red”

I’ve been playing around with if statements but I can’t seem to figure those out. What would be the simplest way to do this?

I thought you couldn’t use ='s in if statements?

@HereComeDatEli
This idea would use no if statements at all and is probably a good idea.

You need a dictionary as such :

numbers = {

 [1] = "orange";
 [2] = "purple";
 [3] = "green";
 [4] = "yellow";

     }

 print(numbers[math.random(1,4)])

math.random takes 2 integers, returns a random value inclusive

Your issue is that math.random is picking a random integer between 1 and 3, 6, 9, 10, or whichever one gets picked. This means it can also pick 2, 4, 5, and basically any number between 1 and 10 that you don’t want.

Make the math.random(1,4). and do:

local chosenNum = numbers[num]
if chosenNum == 3 then
-- stuff
end

That’s not true! You can totally use them, just make sure you do == and not =. Also, for comparison, you can use < or > or <= or >=.

3 Likes

@HereComeDatEli

Also there is no need for multiple if statements, a simple dictionary as I described above would reduce the need for those and improve performance speed, due to fewer criterias while executing. Using multiple if statements for every single criteria is sure to add a couple extra thousand microns to the function.

1 Like

Even your dictionary isn’t really necesarry. Just make num equal to math.random(1,4) and make it so
local chosenNum = numbers[num]

from there see if it is equal to 3.

No,

A dictionary is not very necessary for this purpose, as to provide indexes associated with values.

Yes it is not necessary for numerical indices but it is an example that shows if you were to use string indexes you would have to make a dictionary.

1 Like

But the numbers are important as well as the colors. The numbers have to be used as well as the colors. TheCarbyneUniverse’s script works just fine.

Yes, but in this case she is using the table to define number values. No need to define a different color for each value when you can simply check if it equals 3 and make it red.

Edit: I should clarify that you’re right, if multiple colors are being used then you should use a dictionary as you described. In this case though where all we are doing is making a part Really Red if it equals 3, a simple if statement should do.

1 Like

It works fine as @Y35X suggested if you would shorten my dictionary to an array like:

colors = {"green", "red", "orange"}
local number = math.random(1, #colors) 
 
print(colors[number].." was rolled !")

This way you will keep the number as well as the color.

@TheCarbyneUniverse’ solution may just “work” however my notice was that it involves using multiple if statements for every color, because you would check it like

 if n = 3 then
else
  if n = 4 then
 else
etc...

which would work but be less optimized, his solution was probably to let you know how to compare values but note that that doing this for various values as in your case would take up too much unnecessary lines of code.

2 Likes