1 | local serverplayers = game.Players:GetPlayers() |
2 |
3 | for i,v in pairs (serverplayers) do |
4 | serverplayers.TeamColor = BrickColor.new( "Really red" , "Lime green" ) |
5 | end |
Put the teams in an table by saying:
1 | local teamsTable = game.Teams:GetTeams() --creates a variable that contains all the teams (this type of variable is called a table) |
2 |
3 | local randomTeam = teamsTable [ math.random( 1 ,#teams) ] --picks a random team |
#teams
gives us the number of teams that we have inside game.Teams
math.random()
picks a random number between 1 and the number of teams (#teams) that we have
used together, math.random(1,#teams)
gives us a random number
we can use that random number to get a random team in teamsTable
we worked thru the code together on discord and came up with the following code:
01 | matchIsInProgress = false |
02 |
03 | game.Players.PlayerAdded:Connect( function (player) |
04 | if (#game.Players:GetPlayers() > = 2 and not matchIsInProgress) then |
05 | print ( "there are enough players, start the match" ) |
06 |
07 | matchIsInProgress = true --set the match in progress bool to true |
08 |
09 | --You should probably tell the players somehow that the match is about to start soon here |
10 | wait( 30 ) --wait 30 seconds for intermission |
11 |
12 | --loop thru players and give each player a random team |
13 | local players = game.Players:GetChildren() |
14 |
15 | for i = 1 , #players do |
You have to use BrickColor.random() to get a random brick color.
This is what your script would look like
1 | local players = game.Players -- player service |
2 |
3 | players.PlayerAdded:connect( function (plr) -- runs when a new player joins |
4 | plr.TeamColor = BrickColor.random() -- randomly assigns a player a random team color |
5 | end ) |
If you want to randomize it between two teams, then you do the following:
01 | local players = game.Players -- player service |
02 | local teamColors = { -- an array of team colors in the game |
03 | "Lime green" , -- the first team color |
04 | "Really red" -- the second team color |
05 | } |
06 |
07 | players.PlayerAdded:connect( function (plr) -- runs when a new player joins |
08 | -- assigns the new player a team based on the teams to choose from in teamColors |
09 | plr.TeamColor = BrickColor.new(teamColors [ math.random( 1 ,#teamColors) ] ) |
10 | end ) |
Here is my TDM game script im programming so far...:
01 | --Variables |
02 | local Values = game.ReplicatedStorage.Values |
03 | local S = Values.Status |
04 | local radiersspawn = game.Workspace.RaidersSpawn --A spawn for the team to teleport to. |
05 | local knightsspawn = game.Workspace.KnightsSpawn --A spawn for the other team to teleport to |
06 | local radiersplayers = game.Teams.Raiders:GetPlayers() --Raiders Team |
07 | local knightsplayers = game.Teams.Knights:GetPlayers() --Knights Team |
08 | local serverplayers = game.Players:GetPlayers() --Players |
09 | local Lobbyplayers = game.Teams.Lobby:GetPlayers() --Players in the lobby |
10 |
11 |
12 | while true do --Game Loop |
13 | if game.Players.NumPlayers = = 1 then --You need 2 players to start |
14 | S.Value = "You need 2 (or more) players to start this game!" |
15 | else |