How Do I Exclude Teams From Being Selected From The Team Randomizer?

I’m trying to make it so when people join they get selected a random team, but not specific teams.

local Teams = game:GetService("Teams"):GetChildren()

game.Players.PlayerAdded:Connect(function(player)
	for i,player in pairs(game.Players:GetPlayers()) do
		if player then
			if player.Team == nil then
				local randomTeam = Teams[math.random(1, #Teams)]
				
				player.Team = randomTeam
				
				print(Teams.Name.." Was Selected!")
			end
		end
	end
end)
local Game = game
local Teams = Game:GetService("Teams")
local RedTeam = Teams.RedTeam --Example team.
local TeamsArray = Teams:GetTeams() --Retrieve an array of teams.
table.remove(TeamsArray, table.find(TeamsArray, RedTeam)) --Remove 'RedTeam' from array of teams.

unless im doing it wrong, it dosent seem to choose any of the teams now.
it just sets the player to neutral team.

local Game = game
local Teams = Game:GetService("Teams")
local Team = Teams.Test
local TeamsArray = Teams:GetTeams()
table.remove(TeamsArray, table.find(TeamsArray, Team))

game.Players.PlayerAdded:Connect(function(player)
	for i,player in pairs(game.Players:GetPlayers()) do
		if player then
			if player.Team == nil then
				local randomTeam = Teams[math.random(1, #Teams)]
				
				player.Team = randomTeam
				
				print("random team selected!")
			end
		end
	end
end)

You could make a table with team names which you don’t want players to be added to. So your script would look something like this:

local Teams = game:GetService("Teams"):GetChildren()
local ExcludedTeams = {"Test1", "Test2"}

game.Players.PlayerAdded:Connect(function(player)
	for i,player in pairs(game.Players:GetPlayers()) do
		if player then
			if player.Team == nil then
				local randomTeam = Teams[math.random(1, #Teams)]
				if randomTeam.Name ~= table.find(ExcludedTeams, randomTeam.Name} then
					player.Team = randomTeam
					print(Teams.Name.." was selected!")
				end
			end
		end
	end
end)

it now sets the player as a random team but it, still sets me as the excluded teams.

Sorry for the late response.

Do you want teams of all players to be changed when someone joins?

if you want them to be organized
like for example there is 7 ppl, red will be 4 and blue 3

if you want them to be fully randomized example : red 6 and blue 1