Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 5 years ago

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:

01plr = script.Parent.Parent.Parent.Parent.Parent
02 
03function Click(mouse)
04   plr.TeamColor = BrickColor.new("Really Red")
05   wait(0.1)
06   plr:LoadCharacter();
07end
08 
09 
10script.Parent.MouseButton1Down:connect(Click)
2
Actually attempt it first Shawnyg 4330 — 5y
0
I have attempted it however nothing i've tried has worked so this is the code that I didn't delete. niceboy1419 14 — 5y
0
You would need to put an if statement, to see how many players there are. zandefear4 90 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago

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!!
02local player = game.Players.LocalPlayer
03local button = script.Parent
04local team = game.Teams.Guards --Make a team inside game.Teams instead of using TeamColor
05 
06function 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
16end
17 
18button.MouseButton1Down:Connect(changeTeam)

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

Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago
01--LocalScript
02plr = game.Players.LocalPlayer --Only works in localscripts
03local team1 = game.Teams["TEAM NAME HERE"] --put the team names here
04local team2 = game.Teams["OTHER TEAM HERE"] --I assume you have another team, so put its name there
05 
06 
07function Click(mouse)
08   local team1_plrs = team1:GetPlayers() --Team instances have this function
09   local team2_plrs = team2:GetPlayers()
10 
11 
12   if #team1_plrs <= #team2_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 = team1
View all 24 lines...

This variant aims to retain balance among teams.

Answer this question