I want to make a script like on the darkness game where it chooses a random player and puts them on a certain team how can i do this?
You can also do this in one line.
1 | local randomPlayer = game.Players:GetPlayers() [ math.random( 1 ,#game.Players:GetPlayers()) ] |
Broken down:
game.Players:GetPlayers()
returns a table of all the players in the game
The []
brackets get something from a table, e.g.
1 | local table = { "apple" , "banana" } |
2 | print (table [ 2 ] ) |
3 | > banana |
math.random(arg1,arg2)
returns a random number between arg1 and arg2
#game.Players:GetPlayers()
gets the number of players in the game (# gets the length of a table)
Using the player we've got, we can put them into a team.
1 | local randomPlayer = game.Players:GetPlayers() [ math.random( 1 ,#game.Players:GetPlayers()) ] |
2 | randomPlayer.TeamColor = BrickColor.new( "Teams TeamColor here" ) |