Checking if a team has 0 players

Hello,

I am trying to make a script that detects if there already is a player in a team so another player can not join it. My script is working good but when the player leaves the team the variable is still set to false. How can I make the variable become false when there are 0 players in a team?

My code
local RemoteEventJ = game.ReplicatedStorage.TeamRemotes.Judge
local JAvailible = true

RemoteEventJ.OnServerEvent:Connect(function(player)
	for i, v in pairs(game.Players:GetChildren()) do
		if v.Team ~= game.Teams.Judge then
			wait()
		elseif v.Team == game.Teams.Judge then
			JAvailible = false
		end
	end
	if JAvailible == true then
		if player then
			player.Team = game.Teams.Judge
			player.Character.Humanoid.Health = 0
		end
	elseif JAvailible == false then
		if player then
			player.PlayerGui.Miscellaneous.MaxSry.Visible = true
			wait(2)
			player.PlayerGui.Miscellaneous.MaxSry.Visible = false
		end
	end
end)

Thanks!

if #team:GetPlayers() == 0 then
    --Team is empty
end
7 Likes

So I need to make another elseif or I can just insert that between the if and the for loop?
EDIT: I think you are using the wrong symbol

To me, it looks like the loop is only for checking if a player is in the team, so you can get rid of that loop and use the code I provided for you.

And no, I am not using the wrong symbol. #table is how many things are in the table.

1 Like

I will try it out, thanks.

This is a 30 char message

I need to use this kind of code for my game, but can I use variables or do I have to use a table?

Can you explain what you are trying to do a bit more, please?

You know, the information in a script stored by first entering local and then variable name. followed by an “=” with the stuff in front that on the line being the information stored inside said variable.

EX:

local ExampleVariable = ExampleInfo

For clarity,
If the number of players returned is greater than 0, then the the team isn’t empty.

I believe this was intended

if #team:GetPlayers() == 0 then
    --Team is empty
end
3 Likes

Well, you can do local isEmpty = #team:GetPlayers() == 0 and then the variable would be true if the team is empty and false if not.

Isn’t replying on a post that is already solved and solved a while ago not allowed to reply to?

Yeah, I just read over my reply, and I was confused for a second. Thanks for letting me know about my error!

1 Like