Hello, I would like to have players divided into two teams equally, I have looked everywhere, but none of the solutions work, I tried to use a for loop to split the players into half, but that didnt work either. I would appreciate any help to my problem, thanks.
local Teams = {
game:GetService('Teams'):WaitForChild('Team1');
game:GetService('Teams'):WaitForChild('Team2');
}
local TeamCount = 1;
for _,Player in pairs(game:GetService('Players'):GetPlayers()) do
Player.Team = Teams[TeamCount];
TeamCount++;
if (TeamCount > #Teams) then
TeamCount = 1;
end
end
Alternatively if you’re just using 2 teams, you can modulo %
the index of the for loop by 2, add one to the value, then use that as the index of the ‘Teams’ table:
local Teams = {
game:GetService('Teams'):WaitForChild('Team1');
game:GetService('Teams'):WaitForChild('Team2');
}
for Index,Player in pairs(game:GetService('Players'):GetPlayers()) do
Player.Team = Teams[(Index%2)+1];
end
Hi, thanks for the reply, I have two teams, and it is putting my players into neutral, instead of the teams
Have you ensured that the game:GetService('Teams'):WaitForChild('Team1');
etc. has been updated correctly to reflect on your game’s teams? It’s working fine in my studio.
yes i have, here is my script:
local Teams = {
game:GetService('Teams'):WaitForChild('TeamOne');
game:GetService('Teams'):WaitForChild('TeamTwo');
}
for Index,Player in pairs(game:GetService('Players'):GetPlayers()) do
Player.Team = Teams[(Index%2)+1];
end
Are you waiting until the players have actually joined before changing the teams?
I am using the local server test with 2 players in studio
You’ll need to wait until the Player’s have actually joined. It takes a few seconds.
I’m not sure how you want it to work in your game, is this something you want to update every time a player joins?
Yes, everytime someone joins, it puts them in one of the teams
I waited 30 seconds, and it hasn’t assigned a team yet, the players are still all on neutral
local Teams = {
game:GetService('Teams'):WaitForChild('Team1');
game:GetService('Teams'):WaitForChild('Team2');
}
local function PlayerAdded(Player)
local LowestTeam;
for _,Team in pairs(Teams) do
if (not LowestTeam) then
LowestTeam = Team;
elseif (#Team:GetPlayers() < #LowestTeam:GetPlayers()) then
LowestTeam = Team;
end
end
Player.Team = LowestTeam;
end;
game:GetService('Players').PlayerAdded:Connect(PlayerAdded);
for _,Player in pairs(game:GetService('Players'):GetPlayers()) do
PlayerAdded(Player);
end
If you only have two teams in your game (or only have teams that you want it to sort against):
local function PlayerAdded(Player) -- Every time a Player joins
local LowestTeam; -- Initialize a new variable for holding the 'team with the lowest number of players'
for _,Team in pairs(game:GetService('Teams'):GetChildren()) do -- For every team in the game
if (not LowestTeam) then -- If this is the first loop
LowestTeam = Team; -- Make this team the lowest team
elseif (#Team:GetPlayers() < #LowestTeam:GetPlayers()) then -- If this team has less players than the current 'LowestTeam'
LowestTeam = Team; -- Assign it as the new 'LowestTeam'
end
end
Player.Team = LowestTeam; -- Set the player's team to the lowest team
end);
game:GetService('Players').PlayerAdded:Connect(PlayerAdded);
for _,Player in pairs(game:GetService('Players'):GetPlayers()) do
PlayerAdded(Player);
end
Hi, I tested it, the first player joins and puts them in neutral, and when a second player joins it puts them in one of the teams but, one of them is still in neutral
Also when both player joins at the same time, it puts them both at neutral
I’ve updated my original script. That should fix it.
Isn’t there an AutoAssignable
property on a Team object?
Also just so you know, the ‘AutoAssignable’ property of teams should do most of this for you.
Great minds think alike
This is useful if you’re intending on having multiple teams but only want a selection of them assignable at set points. I think I’m a bit confused by what the intention is.
when i used AutoAssignable, it puts both two players into same team
And AutoAssignable is turned on on both teams?
Make sure your two teams are different colors or else they won’t appear separately on the leaderboard.