Help with removing teams when no one in it

i guess the itle explains itself, what I’m trying ti achieve is on title, so, basically it doesn’t works, it doesn’t parents team to nil, any idea why no? :confused: Can I get some help?
Thanks!

local teamsstorahe = {}
local byeteam = {}
for i,v in ipairs(game.Teams:GetChildren()) do
	teamsstorahe[v.Name] = v
end
plr.ChildAdded:Connect(function(plrrr)
	if plrrr:IsA('Player') then
		table.insert(players,plrrr)
		end
end)

plr.ChildRemoved:Connect(function(plrrr)
	if plrrr:IsA('Player') then
		table.remove(players,plrrr)
		end
end)

while (true) do
	wait()
	if hum.Health < 1 then
		break
	end
	for i,v in ipairs(players) do
		for a,team in ipairs(teamsstorahe) do
			if v.Team ~= team then
				byeteam[team] = true
			end
			if v.Team == team then
				byeteam[team] = true
			end
		end
	end
	for i,v in ipairs(teamsstorahe) do
		if byeteam[v] == true then
			game.Teams[v].Parent = nil
		else
			game.Teams[v].Parent = game.Teams
		end
	end
end

First of all, your script will never break out of the loop because you have not defined the variable “hum”
Also, instead of making a while loop, you can just check when the player leaves: if the team the player was in is now empty.

Use Team.PlayerRemoved and then use #Team:GetPlayers() to get the number of players

2 Likes

I’ve actually done this in one of my games, and it’s relatively easy. All you have to do is create a Folder inside of the Teams service which holds the teams that aren’t in use.

Remove a team with no one in it:

local Teams = game:GetService("Teams")
		
for _, team in pairs(game.Teams:GetDescendants()) do
	if team:IsA("Team")then
		team.PlayerRemoved:connect(function(player)
   	 		local players = team:GetPlayers()
   
			if #players <= 0 then
				team.Parent = game.Teams.NotInUse
			end
		end)	
	end	
end

To add someone to a team, and enable it to show on the leaderboard, just do this:

    local Team = "NameOfTeam"
    local function GetTeamByNameInUnused(name)
    	if game.Teams.NotInUse:FindFirstChild(name) then return game.Teams.NotInUse[name] end
    		for _, team in pairs(game.Teams.NotInUse:GetChildren()) do
    			if team.Name:sub(1, name:len()):lower() == name:lower() then
    			return team
    		end
    	end
    end
	


    local team = GetTeamByNameInUnused(Team)
    		
    if team then
    	team.Parent = game.Teams
    	Player.Team = team
    end

You’re welcome!

4 Likes
local team == game.Teams["TeamNameHere"]
team.PlayerRemoved:Connect(function()
if team:GetChildren() == 0 then
team:Destroy() -- Or move the team to a folder in server storage if you want to keep it and just put it back in teams when a player selects the team.
end
end)
2 Likes

Is it possible to add the team back with touch? This is my script that for touch to change team:

local changedTeam = BrickColor.new("Really black"); 

local part = script.Parent;
local NotEnough = script.Parent.Text2.TextLabel
local Enough = script.Parent.Text.TextLabel

part.Touched:connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent);
	if plr then
		if plr.leaderstats.Bounty.Value < 1200 then
		Enough.Visible = false
		NotEnough.Visible = true
		wait(2)
		Enough.Visible = true
		NotEnough.Visible = false
    else
		plr.leaderstats.Bounty.Value = plr.leaderstats.Bounty.Value - 1200
		plr.TeamColor = changedTeam;
		wait(0.1)
		plr:LoadCharacter()
		wait(5)
       end
	end
end)

Thanks this led to solution to my issue of wanting to turn off teams and turn them back on as part of game doesn’t need teams

here is what I did
2 button driven scripts

ON
local team = game.Teams.oFFline[“Team1”]
team.Parent = game.Teams
local team2 = game.Teams.oFFline[“Team2”]
team2.Parent = game.Teams
local team3 = game.Teams.oFFline[“Lobby”]
team3.Parent = game.Teams
OFF

local team = game.Teams[Team1]
team.Parent = game.Teams.oFFline
local team2 = game.Teams[Team2]
team2.Parent = game.Teams.oFFline
local team3 = game.Teams[Lobby]
team3.Parent = game.Teams.oFFline

you could add the player count logic into this to answer first question but it seems to retain player team colour after and is also possibly only solution I’ve seen to make team leaderboard ‘invisible’ either way it lead me to my solution thanks