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

6 Players on one Team?

I started a war game, I only want 6 players on each team, like if this were to work in a LocalScript..

if game.Teams.TEAMNAME.NumPlayers == 6 and game.Teams.TEAMNAME.PlayerAdded then
	game.Players.LocalPlayer.Team = "TEAMNAME2"
end

PlayerAdded is the thing I'm not sure about. I don't know if it should be there. I need a better script, because, obviously, this doesn't work. There are gonna be 12 players allowed in the game. 6 each team.

Even the NumPlayers part I'm worried about, it's maybe not supposed to be there. The Script Does Not Work

Please help me with this. Thank you.

5 comments

0

or < in the condition statement works better, just fit them into PlayerAdded, that is actually an event. Also, Teams are set by color.

0

What's rval and dval, do I change those two to the teamnames or the change the colors to the team colors?

0

And do I change the numbers next to rval and dval to the number I want the max to be? 6?

0

(i'm sorry for my delay), rval and dval are just number values to say how many players are there in each team. I will make a formal answer.

2 answers

If you are only allowing 12 people into the game at once, then ROBLOX's default Team sorting will not allow for more than 6 players on any team, so long as the number of players in the server never actually gets above 12.

That said, you have to loop through the Players of a game to get how many are in each Team, as Teams are purely organizational: they don't have any methods of their own, regardless of how useful that may be (which is why most developers will design their own Team system, rather than use ROBLOX's).

local TeamColor1 = BrickColor.new("Bright red")
local TeamColor2 = BrickColor.new("Bright blue") --Change these two to reflect your game's TeamColors.

function GetTeams()
	local team1, team2 = {}, {}	
	for _, v in ipairs(Game.Players:GetPlayers()) do
		if v.TeamColor == TeamColor1 then
			table.insert(team1, v)
		elseif v.TeamColor == TeamColor2 then
			table.insert(team2, v)
		else
			error(v.Name .. "'s TeamColor does not match either provided TeamColor.")
		end
	end
	return team1, team2
end

Game.Players.PlayerAdded:connect(function(Player)
	local team1, team2 = GetTeams()
	if #team1 <= #team2 then
		Player.TeamColor = TeamColor1
	else
		Player.TeamColor = TeamColor2
	end
end)

2 comments

0

As of posting this, team is no longer a valid word in my brain.

1

Excellent answer.

teams = game.GetService("Teams")
team1 = teams.Team1 -- just for example, this will be the "Really red" team
team2 = teams.Team2 -- and this the "Really blue"

game.Players.PlayerAdded:connect(function(player) -- when a player enters
	team1players = team1:GetChildren() -- get the players in that team
	team2players = team2:GetChildren() -- same
	if #team1players > #team2players then -- if team1 has more than team2
		player.TeamColor = BrickColor.new("Really blue") -- the player goes to team2
	elseif #team1players < #team2players then -- if team2 has more than team1
		player.TeamColor = BrickColor.new("Really red") -- the player goes to team1
	else -- the teams are equal
		player.TeamColor = BrickColor.new("Really blue") -- the player goes to team2, or team1 (which one you want, just because they're already balanced)
	end
end)

Observation: players have the TeamColor property, teams are separated in colors.

1 comment

0

I think this is a bit simpler than the other answer.

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