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

How to randomly print a string.?

Asked by 7 years ago

I was just testing some stuff for a project and I got confused with randomly printing strings. The problem that I get is that it is printing its table value/order (Ie. [1], [2] etc) How would I make it print the string

local test = {

"Test1",

"Test2"

"Test3"

}

local random = math.random(1, #strings)

print(random)

1 answer

Log in to vote
1
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
7 years ago

You are very close! First thing, you messed up your table. You have to put a comma after each index, and you forgot one after the 2nd. Secondly, when trying to get a random value from a table, you have to first index the table!

local test = {"Test1","Test2","Test3"}

local random = test[math.random(#test)] -- The # operator represents how many INDEXES (values) are inside the table!.
print(random)
0
Thanks :D dustyripjaw 2 — 7y
Ad

Answer this question