Checking if a team has a player in it not working

I’m making a game, and when a player joins, the game checks if a team already has a player inside it, and if so, the new player’s team is changed. I tried checking if my script works but it looks like it doesn’t, even tho i’m pretty sure this is how you would do what I’m trying to obtain. This is my script:

local teams = game:GetService("Teams")
local Team1 = teams.Plr1
local Team2 = teams.Plr2
local Team3 = teams.Plr3
local Team4 = teams.Plr4
if #Team1 == 1 then--1
		if #Team2 == 1 then--2
			if #Team3 == 1 then--3
				plr.Team = teams.Plr4
			else--3
				plr.Team = teams.Plr3
			end--3
		else--2
			plr.Team = teams.Plr2
		end--2
	else--1
		plr.Team = teams.Plr1
	end--1

(I use --1, --2 to keep in mind to which if the ends are linked to)
No errors in the output, the script is inside a game.Players.PlayerAdded:Connect(function(plr)
end)
function, inside a server script that I did put in ServerScriptService. Any explaining is appreciated, since I never really worked with Teams in any of my games. I feel bad asking for help to others, but I really don’t understand certain things when it comes to scripting.

Heyo, late response but if you have not figured it out yet here you go.

The reason your code does not work is because you need to use the :GetPlayers method to get the amount of players in a certain team. Right now you’re just putting a # next to your Team variable. # gets the length of a table, while :GetPlayers returns a table of all the present players. That is why if you were to do #Team1:GetPlayers() you would get the amount of players on Team1. This code should work in theory, but no guarantees :wink::

game.Players.PlayerAdded:Connect(function(plr)

local Team1 = game.Teams.Plr1
local Team2 = game.Teams.Plr2
local Team3 = game.Teams.Plr3
local Team4 = game.Teams.Plr4

local Team1Players = #Team1:GetPlayers()
local Team2Players = #Team2:GetPlayers()
local Team3Players = #Team3:GetPlayers()
local Team4Players = #Team4:GetPlayers()

	if Team1Players == 0 then 
		plr.Team = Team1
		print("placed on team1")
	
	elseif Team2Players == 0 then 
		plr.Team = Team2
		print("placed on team2")
		
	elseif Team3Players == 0 then
		plr.Team = Team3
		print("placed on team3")
		
	elseif Team4Players == 0 then
		plr.Team = Team4
		print("placed on team4")
		
	elseif Team4Players > 0 then
			print("no space")	
	end	
end)

Also you don’t need to do teams.Plr1 as you already set variable Team1 to teams.Plr1 so you could just use the name of the variable to change the player’s team.

Good luck!

hey! Thanks for the answer, I really appreciate that! Unfortunately tho, the script seems to not work. It gives me an error in the output, “attempt to call a nil value”. I guess that’s not how you count a team’s amount of players. I’ll try looking at the forum for a thread made in the past that might help me. Thanks again for your time!

Odd :huh: Are you sure you put in the code correctly? Without any other lines?

First of all, that’s not how you get the length of player’s in a Team. You are doing an invalid method in a string

In which it should be:

local Team1Players = Team1:GetPlayers()
local Team2Players = Team2:GetPlayers()
local Team3Players = Team3:GetPlayers()
local Team4Players = Team4:GetPlayers()

Anyways, feel free to read the document here:

It works fine, my bad. My error was that I put :GetPlayers() in the Teams variables, so I was repeating two times the :GetPlayers(). It seems to work now! Thanks, I also understood how to actually use the “#” symbol.

1 Like

What you did would result in an error, ‘‘Team1Players’’ is a set variable which gets the amount of players in the team, therefore the #. What you’re doing is just setting Team1Players to get the table, not the table length.

1 Like

Ooh my bad, seems like I misread that part. Forgot that it returns a table. :sad:

1 Like