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

Randomized teams staying even?

This is my attempt at trying to make a function that gets all the players in the game and putting them on teams and making sure that even amounts of players are on each team. The problem is tho if another player joins after the game has started they arent put on a team and cant play till the next round. However, I want the new player to still be sorted into a team, without changing the current teams, just adding them to a team, keeping them even.

function getTeams()
    local teamNumber = 1
    repeat wait()       
        local lobby = {}
        local teams = {"Red", "Blue"}
        for _, player in pairs(game.Players:GetPlayers()) do
            if player.TeamColor == game.Teams["Lobby"].TeamColor then
                local character = player.Character
                if character and character:FindFirstChild("Torso") then
                    table.insert(lobby, player)
                end
            end
        end

        local randomPlayer = lobby[math.random(1, #lobby)]
        randomPlayer.TeamColor = game.Teams[teams[teamNumber]].TeamColor
        toMap(randomPlayer)

        local lobbyNumber = 0
        for _, player in pairs(game.Players:GetChildren()) do
            if player.TeamColor == game.Teams["Lobby"].TeamColor then
                if player.Character:FindFirstChild("Torso") then
                    lobbyNumber = lobbyNumber + 1
                end
            end
        end
        teamNumber = teamNumber == 1 and 2 or 1
    until lobbyNumber == 0
end

1 comment

0

Always add new players in the team with the fewest players. If there is a tie, flip a coin (generate a random number) and make a decision based on that.

2 answers

You're making it more complicated than it has to be. All you really need to is just have an event when a players join, and if theres a game going on, itll only put that player in.

Something like this:

game.Players.PlayerAdded:connect(function(p)
	if gameGoing == true then --just checking to see if a game is going
		local teams = {"Red", "Blue"}
		p.TeamColor = game.Teams[teams[math.random(1,#teams)]].TeamColor
	end
end)

2 comments

0

or you could use p.Team = game.Teams[teams[math.random(1,#teams)]

0

The problem with that is 1. It picks team randomly, meaning that technically, every player could be put on one team. 2. It only puts them on teams when they join. Im using a team function, so I can call it, cause I call it in the middle of a game loop

I made a little function for this, here you go! -- Should be in the middle of a script.

		local red = Instance.new("Team", game.Teams)
		red.Name = "Red Team"
		red.TeamColor = BrickColor.new("Really red")
		local blue = Instance.new("Team", game.Teams)
		blue.Name = "Blue Team"
		blue.TeamColor = BrickColor.new("Bright blue")
		local half = game.Players.NumPlayers / 2
		local redcount = 0
		local bluecount = 0
		for i, player in pairs(game.Players:GetPlayers()) do
			if redcount ~= half then
				player.Neutral = false
				player.Team = red
				redcount = redcount + 1
			else
				player.Neutral = false
				player.Team = blue
				bluecount = bluecount + 1
			end
		end

and heres the add player to team if they just joined function:

game.Players.PlayerAdded:connect(function(plr)
	local blueteam = 0
	local redteam = 0
	for i,x in pairs(game.Players:GetChildren()) do -- Get the players and add the numbers.
		if x.Team == "Blue" then
			blueteam = blueteam + 1
		elseif x.Team == "Red" then
			blueteam = blueteam + 1
		end
	end
	if blueteam > redteam then
		plr.Team = "Red"
	elseif blueteam < redteam then
		plr.Team = "Blue"
	end
end)

1 comment

0

Gets all the players in the server and puts them into even teams.

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