How to change the team of all players in another team

So let’s say we have two teams:
Team A and Team B

There is currently an unknown number of players in Team A
How do I assign all of the players that are in Team A to become players in Team B?

2 Likes

This is how you would do it. You would check every player to see if they are on Team A and then it will assign them to Team B just like this:

for i,v in pairs(game.Players:GetChildren()) do
	if v.Team == game.Teams["Team A"] then
		v.Team = game.Teams["Team B"]
	end
end

(Just a quick thing Teams are considered an Object not a String)

2 Likes

Quick mention that Teams also has a GetPlayers method (don’t use GetChildren on Players) so you can shorten the work and avoid reinventing the wheel for it by directly getting the players on the team and operating on them. This cuts down on the iterations you do as well so that you are strictly only performing iterations against players who meet your criteria.

local Teams = game:GetService("Teams")

-- Use ipairs to iterate over arrays
for _, player in ipairs(Teams["Team A"]:GetPlayers()) do
    player.Team = Teams["Team B"]
end
3 Likes