Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would you call upon a value at random?

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?

2 comments

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.

0

it doesn't work.

2 answers

✓ Accepted answer

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)]

1 comment

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.

Yes that would work like this

 target = math.random(1,3)
print(target)

1 comment

0

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.