✓ Accepted answer
local plevents = {sword, sniper}
local chosen = plevents[1]
the "plevents" table has two items, sword and sniper.
sword is 1, and sniper is 2. so if you do this
print(plevents[1])
print(plevents[2])
the output will be like this :
sword
sniper
But if you do this
local plevents = {sniper, sword}
local chosen = plevents[1]
Then sniper is 1, and sword is 2. so if you do this
print(plevents[1])
print(plevents[2])
The output will be like this
sniper
sword
Now using this code
print(math.random(1,#plevents))
is basically the same as this
print(math.random(1,2))
that is because the table "plevents" has 2 items
and the output of the code above is this
1
or this
2
Now, using this code
print(plevents[math.random(1,#plevents)])
the output will be :
Sword
or
Sniper
Remeber
plevents[1] is the sword
plevents[2] is the sniper
and that
math.random(1,#plevents)
is basically like math.random(1,2)
the 2 in math.random(1,2) is the amount of items there is in the table "plevents" using it will either get you 1 or 2
now im gonna simplify it
print(plevents[math.random(1,#plevents)])
=print(plevents[math.random(1,2)])
=print(plevents[1]) or print(plevents[2])
=Sword or Sniper
Do you get it now? i hope so.
1 comment
Use math.random(1,#plevents). It will choose a number between 1 and the number of items in the table. (In your case, 2)