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

How to prevent players from joining a team that is full?

I am making a prison style game and before the player is placed into the game they are prompted with a GUI that allows them to choose guards or prisoners. How can I make it that when the guard team reaches 11 players (the max amount of guards I want) if a player clicks the guard choice they will instead be given a message saying team full instead of being spawned in as a guard. Here is my code:

plr = script.Parent.Parent.Parent.Parent.Parent

function Click(mouse)
   plr.TeamColor = BrickColor.new("Really Red")
   wait(0.1)
   plr:LoadCharacter();
end


script.Parent.MouseButton1Down:connect(Click)

3 comments

2

Actually attempt it first

0

I have attempted it however nothing i've tried has worked so this is the code that I didn't delete.

0

You would need to put an if statement, to see how many players there are.

2 answers

✓ Accepted answer

There is a function called :GetPlayers() which is used on a team. You can also use a # to get the length of something. Example:

--Make sure this is a LocalScript!!
local player = game.Players.LocalPlayer
local button = script.Parent
local team = game.Teams.Guards --Make a team inside game.Teams instead of using TeamColor

function changeTeam()
	local players = team:GetPlayers() --Gets the players in the Team
	
	if #players < 11 then --Makes sure that the amount of players in the Team is less than a number (11)
		player.Team = team --Changes the player's team
	else
		button.Text = 'Team Full!'
		wait(1)
		button.Text = team.Name --Changes the button's text to the name of the Team
	end
end

button.MouseButton1Down:Connect(changeTeam)

Hope this helps! Don't forget to accept this answer if it does, comment if you have any questions.

--LocalScript
plr = game.Players.LocalPlayer --Only works in localscripts
local team1 = game.Teams["TEAM NAME HERE"] --put the team names here
local team2 = game.Teams["OTHER TEAM HERE"] --I assume you have another team, so put its name there


function Click(mouse)
   local team1_plrs = team1:GetPlayers() --Team instances have this function
   local team2_plrs = team2:GetPlayers()
   
   
   if #team1_plrs <= #team2_plrs then --putting a '#' before a list/array returns the ammount of entries in said list/array.
	
   	wait(0.1)
	plr.Team = team1
   	plr:LoadCharacter();
   else
         --put code here if you want, or else remove the else statement
   end

end


script.Parent.MouseButton1Down:Connect(Click)

This variant aims to retain balance among teams.

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