Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to I make a team randomizer script? (Help)

local serverplayers = game.Players:GetPlayers()
	
	for i,v in pairs(serverplayers) do
		serverplayers.TeamColor = BrickColor.new("Really red", "Lime green")
	end

3 answers

✓ Accepted answer

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

#Explaining the second line of code:

#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

#edit: 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)

3 comments

0

Ive explained my whole script below

0

ty

0

idk why your answer got removed as answer unless it was op removed it as answer or another mod did i just added note to your thread. no clue if your solution works or not because i've never tried changing team with team color

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)

1 comment

0

Ive shown my whole script below

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

2 comments

0

Sorry this is kinda hard to explain. I hope you understand

0

Go to studio and then click model, then in model click services and then click Teams. Once you have Teams on explorer, insert a team and play with the properties. You can assign a name to a team color using this.

This discussion is preserved as it was. New answers, comments, and votes are closed.