Answered by
7 years ago Edited 7 years ago
A mistake that I have caught is that you did not put a comma(,) or a semicolon(;) after each string. That is very important to have so you could access those strings. Another thing you must know is that if you put # before a table name, that gets the number of keys (in this case strings) in your table. The final thing you should know that if you want to get the strings in your table or access anything in the table, you would have to do Strings[Which string you want to get]
. You have 3 strings, so you are able to put in between the brackets 1, 2 or 3. Here is your fixed script.
1 | local Strings = { "line1" , "line2" , "line3" } |
2 | local selectedIndex = math.random( 1 ,#Strings) |
3 | local selectedString = Strings [ selectedIndex ] |
4 | print ( 'Selected String is ' .. selectedString) |
5 | print ( 'Selected Index is ' .. selectedIndex) |
If you wanted to make things truly random, you would put math.randomseed(tick()) on the top of your script.
2 | local Strings = { "line1" , "line2" , "line3" } |
3 | local selectedIndex = math.random( 1 ,#Strings) |
4 | local selectedString = Strings [ selectedIndex ] |
5 | print ( 'Selected String is ' .. selectedString) |
6 | print ( 'Selected Index is ' .. selectedIndex) |
Please accept my answer if it fixed your script. If not, please ask questions in the comments.