• Choosing random player with player.GetAll()?
    5 replies, posted
How would I choose one random player out of the entire list? Thanks.
[code]local randomply = player.GetAll()[math.random(1,#player.GetAll())][/code]
Even easier: [lua] local randomply = table.Random(player.GetAll()) [/lua]
[QUOTE=Gbps;16045760]Even easier: [lua] local randomply = table.Random(player.GetAll()) [/lua][/QUOTE] dude i didnt even know that function existed
Gbps because the player.GetAll() table has a sequential numerical index it would be better to use Entoros code. They both work but Entoros is just better PR for that type of thing. Example of Entoros code. [lua] local rply = math.random(1, #player.GetAll()) -- Yes we can use 1 because lua doesn't start from 0 like traditional C# local chosen = player.GetAll()[rply] for i = 1, #player.GetAll() do player.GetAll()[i]:PrintMessage(3, "The chosen one is "..chosen); end [/lua]
Would it just be easier with [lua]local randomply = table.Random(ply.GetAll())[/lua]
Sorry, you need to Log In to post a reply to this thread.