local serverplayers = game.Players:GetPlayers() for i,v in pairs(serverplayers) do serverplayers.TeamColor = BrickColor.new("Really red", "Lime green") end
Put the teams in an table by saying:
local teamsTable = game.Teams:GetTeams() --creates a variable that contains all the teams (this type of variable is called a table) 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:
matchIsInProgress = false game.Players.PlayerAdded:Connect(function(player) if(#game.Players:GetPlayers() >= 2 and not matchIsInProgress)then print("there are enough players, start the match") matchIsInProgress = true --set the match in progress bool to true --You should probably tell the players somehow that the match is about to start soon here wait(30) --wait 30 seconds for intermission --loop thru players and give each player a random team local players = game.Players:GetChildren() for i = 1, #players do local teamsTable = game.Teams:GetTeams() --creates a variable that contains all the teams (this type of variable is called a table) local randomTeam = teamsTable[math.random(1,#teamsTable)] --picks a random team players[i].Team = randomTeam end end end)
You have to use BrickColor.random() to get a random brick color.
This is what your script would look like
local players = game.Players -- player service players.PlayerAdded:connect(function(plr) -- runs when a new player joins plr.TeamColor = BrickColor.random() -- randomly assigns a player a random team color end)
If you want to randomize it between two teams, then you do the following:
local players = game.Players -- player service local teamColors = { -- an array of team colors in the game "Lime green", -- the first team color "Really red" -- the second team color } players.PlayerAdded:connect(function(plr) -- runs when a new player joins -- assigns the new player a team based on the teams to choose from in teamColors plr.TeamColor = BrickColor.new(teamColors[math.random(1,#teamColors)]) end)
Here is my TDM game script im programming so far...:
--Variables local Values = game.ReplicatedStorage.Values local S = Values.Status local radiersspawn = game.Workspace.RaidersSpawn --A spawn for the team to teleport to. local knightsspawn = game.Workspace.KnightsSpawn --A spawn for the other team to teleport to local radiersplayers = game.Teams.Raiders:GetPlayers() --Raiders Team local knightsplayers = game.Teams.Knights:GetPlayers() --Knights Team local serverplayers = game.Players:GetPlayers() --Players local Lobbyplayers = game.Teams.Lobby:GetPlayers() --Players in the lobby while true do --Game Loop if game.Players.NumPlayers == 1 then --You need 2 players to start S.Value = "You need 2 (or more) players to start this game!" else S.Value = "Intermission" --Start game when we have 2 players wait(.1) S.Value = "Randomizing teams...." wait(2) local Teams = { --Teams Tabel "Lime green", "Really red" } for i,v in pairs(Lobbyplayers) do v.TeamColor = BrickColor.new(Teams[math.random(1,#Teams)]) --I dont mean by putting all the players in a random team what I mean is --Moving random players to random teams by teams I mean the "Raiders Team" and the "Knights Team" --Cause im making a TDM game --This is the part im struggling with. end end end