How Would I Set The Rest Of The Players On Another Team?

I am trying to make it so I can have one person on the “King Boo” team, One on the “Luigi” team, and finally the rest on will go on the Boo team. I looked at videos and articles for help, but I just couldn’t find anything. Here is how far I have gotten, I can get a person on the “King Boo” and “Luigi” team, but not the boo team. Those people just stay on the lobby team and don’t change at all.

while true do
	wait(10)
	local players = game.Teams.Lobby:GetPlayers()
	local Teams = game.Teams
	local L = Teams.Luigi
	print(#players)
	if #players >= 3 then
		local KingBoo = players[math.random(1,#players)]
		KingBoo.Team = Teams["King Boo"]
		KingBoo:LoadCharacter()
		wait(1)
		local Luigi = players[math.random(2,#players)]
		Luigi.Team = L
		Luigi:LoadCharacter()
		wait(1)
		local Boo = players
		Boo.Team = Teams["Boo's"]
        Boo:LoadCharacter()
		wait(120)
	end
end

Any help on this is very appreciated! :grin: Also examples and articles help a lot.

To do this, you’d want to grab a list of players and then remove selected ones from the table while picking various players. Here’s a code example:

local teams=game:GetService("Teams")

local function sortTeams()
	local players=teams.Lobby:GetPlayers() -- get an array of players in the Lobby team
	
	-- choose Luigi
	do
		local index=math.random(1,#players) -- choose a random index between 1 and the length of the table
		players[index].Team=teams["Luigi"] -- set the randomly chosen player to the Luigi team
		table.remove(players,index) -- removes them from the pool of players, ensuring no duplicate picking later
	end
	
	-- choose Boo
	do
		local index=math.random(1,#players)
		players[index].Team=teams["King Boo"]
		table.remove(players,index)
	end
	
	-- sort remaining players
	do
		table.foreach(players,function (_,player) player.Team=teams["Boo's"] end)
	end
end

sortTeams()
1 Like

Thank You So Much It Worked! I am going to have to start studying tables more now. I will look for an article online to study on, if you have any other good examples, could you please provide them as well, thanks! :grin:

Tables can be tricky to start off with, but like with everything they can be mastered with a bit of time and practice.

If you want to learn more about what tables are capable of I’d suggest looking around the wiki. If you want to learn a little about table manipulation like my above example, give this wiki page a read.

Best of luck!

I have been programming for a little bit and I think it is time for a challenge, tables will also be useful to some data stores, thanks for everything you gave me. (now all I have left to do that is hard is to change the player’s character into my custom models).

is a script or local script and where do I put it cause I need this to!

In a (server)script, best to put it in ServerScriptService as well, but can go about anywhere.