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?
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)]
Thanks, but the prints were just placeholder for larger scripts that I didn't want to confuse anyone with. The 'randomTarget' works just as I need it to.
Yes that would work like this
target = math.random(1,3)
print(target)
no, i specifically need target1, target2, and target3. im wondering how you CALL upon them randomly
This discussion is preserved as it was. New answers, comments, and votes are closed.
2 comments
I don't think so, because you've assigned target1, target2 and target3, but not target itself. However, you should try it in the Studio - I would but my Studio is upgrading.
it doesn't work.