Can't destroy a team!

Script

Players.PlayerRemoving:Connect(function(player)
	for i, team in pairs(game:GetService("Teams"):GetTeams()) do
		if #team:GetPlayers() == 0 then
                        print("Player left!")
			team:Destroy()
		end
	end
end)

Print

  19:17:52.851   ▶ Player left! (x8)  -  Server - Script:10

It runs but it doesnt destroys the team for some reason :thinking:

You can use this function (Team | Roblox Creator Documentation) instead of Players.PlayerRemoving, see if that changes the outcome.

Okay, will try right now! Will reply !

Still doesn’t work!

team.PlayerRemoved:Connect(function()
		if #team:GetPlayers() == 0 then
			team:Destroy()
		end
	end)

It might be a ROBLOX bug not really sure :l

Seems to be working fine for me. Are you changing team on the client-side?

Also, here’s an updated version I made.

local Teams = game:GetService('Teams')
local GetTeams = Teams:GetChildren()

for i = 1, #GetTeams do
	local Team = GetTeams[i]
	Team.PlayerRemoved:Connect(function(Player)
		print(('Player %s has left the team %s.'):format(Player.Name, Team.Name))
		if #Team:GetPlayers() == 0 then
			Team:Destroy()
			print(('Team %s has been destroyed.'):format(Team.Name))
		end
	end)
end

I am running it server-sided and I am not sure why its not working. It might be a bug :angry:

Works fine for me, this is a server-script, right?
It should be.

Yeah its server-sided! No local scripts are interacting with the teams… I am getting crazy hahah this is making me mad because it should work 100%

Where do you change your team? If there’s a script, show me.
Does it at least print anything?

One sec, will check if it really prints this time :slight_smile:

Yeah it prints, but still isnt deleting the team :confused:

local team = GetTeamFromPlayer(player)
	local soundString = Instance.new("StringValue", team)
	soundString.Name = "Sound"
	soundString.Value = sound
	local animationString = Instance.new("StringValue", team)
	animationString.Name = "Animation"
	animationString.Value = animation
	
	team.PlayerRemoved:Connect(function()
		if #team:GetPlayers() == 0 then
			print("DESTROYED TEAM!")
			team:Destroy()
		end
	end)

It detects when a player is removed from the team so I dont know tbh :l

Try using my script instead, and — I don’t know what GetTeamFromPlayer is.
Also, you shouldn’t use the parent argument with Instance.new if you are modifying its properties afterwards.

Yeah will try rn! If it doesnt work it might be because of lag :frowning:

GetTeamFromPlayer is my function, it works hahah
still doesnt work might be something wrong. Used your script, might be the game, thank you for the help! I will try to fix it

If my script doesn’t work then there’s definitely something wrong on your side, I cannot help you if that’s the case since everything else seems to be correct.

Understandable, I will try to find it :confused:

You’re doing it wrong. Here is a fixed script.

game.Players.PlayerRemoving:Connect(function(player)
for i,team in pairs(game:GetService("Teams"):GetChildren()) do
	if #team:GetPlayers() <= 0 then
                    print("Last Player left!")
		team:Destroy()
	end
end
end)

Have fun scripting. and also, if this is not the full script post the full script as well. Loops without Spawn functions will yield scripts.