If I had these guys..
target1 = print("1") target2 = print("2") target3 = print("3")
How would I call upon them randomly?
target..math.random(1,3)
would this work?
The print function does not return anything, so target1, target2, and target3 are all equal to nil.
'random' is a function in the math library that generates a random number inside an interval. When you give it the arguments 1, 3
, it returns an integer (no decimals) inside the inclusive interval 1 and 3 (i.e. 1, 2, or 3).
If you wanted to print that value we could do this:
print(math.random(1, 3))
If you wanted to obtain a random variable, we would need to store all the variables in a table and choose a random index:
local target1 -- whatever target1 is local target2 -- whatever target2 is local target3 -- whatever target3 is local targets = {target1, target2, target3} local randomTarget = targets[math.random(#targets)]
Yes that would work like this
target = math.random(1,3) print(target)