I want to insert two random players into a table, and check if they are in the table before they are put in, so that noone is put in the table twice. I have tried making this script, but I have no idea if it works.
1 | if #contestants = = 8 or #contestants = = 9 or #contestants = = 10 then |
2 | for _, randomplayers in pairs ( 2 * math.random( 1 , #contestants)) do |
3 | if randomplayers and randomplayers.Health > 0 then |
4 | table.insert(robber, randomplayers) |
5 | end |
6 | end |
Thanks!
It is best to create a function to see if a value is in the table.
01 | function InTable(tab,val) |
02 | for _,n in pairs (tab) do |
03 | if n = = val then |
04 | return true |
05 | end |
06 | end |
07 | end |
08 |
09 | if contestants = = 8 or contestants = = 9 or contestants = = 10 then |
10 | local RandomPlayer = contestants [ math.random( 1 ,#contestants) ] |
11 | if not InTable(robber,RandomPlayer) then |
12 | table.insert(robber,RandomPlayer) |
13 | end |
14 | end |
I believe I have solved it! If anyone else is wondering about the same thing, here is the script I made. Not sure if it works yet but I havee my fingers crossed (:
1 | if #contestants = = 8 or #contestants = = 9 or #contestants = = 10 then |
2 | local randomplayer 1 = contestants [ math.random( 1 , #contestants) ] |
3 | while true do |
4 | local randomplayer 2 = contestants [ math.random( 1 , #contestants) ] |
5 | if randomplayer 2 ~ = randomplayer 1 then |
6 | table.insert(robber, randomplayer 1 , randomplayer 2 ) |
7 | break |
8 | end |
9 | end |