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:
01 | plr = script.Parent.Parent.Parent.Parent.Parent |
02 |
03 | function Click(mouse) |
04 | plr.TeamColor = BrickColor.new( "Really Red" ) |
05 | wait( 0.1 ) |
06 | plr:LoadCharacter(); |
07 | end |
08 |
09 |
10 | script.Parent.MouseButton 1 Down:connect(Click) |
There is a function called :GetPlayers()
which is used on a team. You can also use a # to get the length of something. Example:
01 | --Make sure this is a LocalScript!! |
02 | local player = game.Players.LocalPlayer |
03 | local button = script.Parent |
04 | local team = game.Teams.Guards --Make a team inside game.Teams instead of using TeamColor |
05 |
06 | function changeTeam() |
07 | local players = team:GetPlayers() --Gets the players in the Team |
08 |
09 | if #players < 11 then --Makes sure that the amount of players in the Team is less than a number (11) |
10 | player.Team = team --Changes the player's team |
11 | else |
12 | button.Text = 'Team Full!' |
13 | wait( 1 ) |
14 | button.Text = team.Name --Changes the button's text to the name of the Team |
15 | end |
16 | end |
17 |
18 | button.MouseButton 1 Down:Connect(changeTeam) |
Hope this helps! Don't forget to accept this answer if it does, comment if you have any questions.
01 | --LocalScript |
02 | plr = game.Players.LocalPlayer --Only works in localscripts |
03 | local team 1 = game.Teams [ "TEAM NAME HERE" ] --put the team names here |
04 | local team 2 = game.Teams [ "OTHER TEAM HERE" ] --I assume you have another team, so put its name there |
05 |
06 |
07 | function Click(mouse) |
08 | local team 1 _plrs = team 1 :GetPlayers() --Team instances have this function |
09 | local team 2 _plrs = team 2 :GetPlayers() |
10 |
11 |
12 | if #team 1 _plrs < = #team 2 _plrs then --putting a '#' before a list/array returns the ammount of entries in said list/array. |
13 |
14 | wait( 0.1 ) |
15 | plr.Team = team 1 |
This variant aims to retain balance among teams.