I want to shorten math.random without using a lot of if / then statements but I simply cant
this is the a part of the code:
if random == 1 then
waiting(id1)
elseif random == 2 then
waiting(id2)
elseif random == 3 then
waiting(id3)
elseif random == 4 then
waiting(id4)
elseif random == 5 then
waiting(id5)
elseif random == 6 then
waiting(id6)
elseif random == 7 then
waiting(id7)
elseif random == 8 then
waiting(id8)
elseif random == 9 then
waiting(id9)
elseif random == 10 then
waiting(id10)
elseif random == 11 then
waiting(id11)
end
I want it to be like this:
waiting(id .. random)
(random is math.random(1, 11) and the ids are image ids)
This is what I came up with. Basically, instead of your program making a random number and then going through a ton of if statements till it finds a match, the following script allows your program to create the random number, and then automatically match that number with the index of the table.
For example, the tables has, " A string " for the 1st index. The 1st index is whatever is wrote first, the second is right after first, on & on.
You will be able to put your ID’s in place of the index numbers, so {111,222,333,444}, and whenever you run the program your random number will be matched with the index number to get you the ID you wanted for that number.
In this example, I used the print function, but if you use this same exact outline, then it will work with any other statement.
local testArray = {"A string", "3.14159", "Hello", "New string"}
local random = math.random(1,4)
print(testArray[random])