To do this, you would need to get the random player, from a directory that you create. In order to achieve that, use the following code :
1 | playerDir = game.Players:GetPlayers(); |
2 | chosen = playerDir [ math.random( 1 , #playerDir) ] ; |
From there, we need to place the chosen player into a table for referencing later on.
2 | table.insert(red, chosen.Name); |
Now, teaming players. We'll use a forloop to index the players and iterate through them to team them respectively.
1 | for index,value in next , game.Players:GetPlayers() do |
2 | if not (value.Name = = red [ 1 ] ) then |
3 | value.TeamColor = game.Teams [ "Blue team" ] .TeamColor; |
4 | else value.TeamColor = game.Teams [ "Red team" ] .TeamColor; |
The above code checks if the player is in the table and if not, teams them blue, else teams them red.
We may want to place it into a function if we need to repeatedly call it, maybe at the end/beginning of rounds.
02 | local playerDir = game.Players:GetPlayers(); |
03 | local chosen = playerDir [ math.random( 1 , #playerDir) ] ; |
05 | table.insert(red, chosen.Name); |
06 | for index,value in next , game.Players:GetPlayers() do |
07 | if not (value.Name = = red [ 1 ] ) then |
08 | value.TeamColor = game.Teams [ "Blue team" ] .TeamColor; |
09 | else value.TeamColor = game.Teams [ "Red team" ] .TeamColor; |
Now to call the function, just use :
The whole script is :
02 | local playerDir = game.Players:GetPlayers(); |
03 | local chosen = playerDir [ math.random( 1 , #playerDir) ] ; |
05 | table.insert(red, chosen.Name); |
06 | for index,value in next , game.Players:GetPlayers() do |
07 | if not (value.Name = = red [ 1 ] ) then |
08 | value.TeamColor = game.Teams [ "Blue team" ] .TeamColor; |
09 | else value.TeamColor = game.Teams [ "Red team" ] .TeamColor; |
Hope this helps.