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

How would you call upon a value at random?

Asked by 7 years ago

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?

0
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. kudorey619 138 — 7y
0
it doesn't work. jiggiypuffs 30 — 7y

2 answers

Log in to vote
0
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
7 years ago
Edited 7 years ago

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)]
0
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. jiggiypuffs 30 — 7y
Ad
Log in to vote
0
Answered by 7 years ago

Yes that would work like this

 target = math.random(1,3)
print(target)
0
no, i specifically need target1, target2, and target3. im wondering how you CALL upon them randomly jiggiypuffs 30 — 7y

Answer this question