Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Checking if team has no players?

Here is my code, no errors it just doesn't work.

while true do
	wait(5)
	t = game.Teams:GetChildren() --getting all teams
	for i,v in ipairs(t) do 
    	if v.Name ~= 'Civilian' then --Don't remove the Civ team
			local teamNumber = v:GetPlayers() --Getting players in the team
			if teamNumber == 0 then --make sure no one is on the team
				v.Parent = game.ServerScriptService.Teams -- move it back into the script
			end
		end
	end
end

2 answers

✓ Accepted answer

Quick code change.

            local teamNumber = v:GetPlayers() --Getting players in the team
            if teamNumber == 0 then --make sure no one is on the team

Should be.

            local teamNumber = #v:GetPlayers() --Getting players in the team
            if teamNumber == 0 then --make sure no one is on the team

Also-- I highly suggest to not do these checks in a while loop. I'm of the opinion that if you can tie it to an event, then you should do so. One way you could do that with this code to use the PlayerRemoved event of each Team.

1 comment

0

Thanks for the help :)

Disclaimer: If you have any errors with this code, I looked up on the wiki only, I haven't tested it out even.

The :GetPlayers() returns an array (objects which are players), you cannot use numbers with it However you can use a for loop to iterate it

while true do
    wait(5)
    t = game:GetService("Teams"):GetChildren() --getting all teams
    for i,v in ipairs(t) do 
        if v.Name ~= "Civilian" then --Don't remove the Civ team
            local teamNumber = v:GetPlayers() --Getting players in the team
	   for playersAmount,_ in ipairs(teamNumber) do 
            	if playerAmount == 0 then --make sure no one is on the team
                	v.Parent = game.ServerScriptService.Teams -- move it back into the script
          		  end
		end
        end
    end
end

If this answered your question, please accept my answer If not, please comment and ask questions

1 comment

0

The playersAmount variable would be a player object. You cannot check a player object against a zero. He is seeing if the length of the players array is equal to zero.

This discussion is preserved as it was. New answers, comments, and votes are closed.