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 4 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:

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)
2
Actually attempt it first Shawnyg 4330 — 4y
0
I have attempted it however nothing i've tried has worked so this is the code that I didn't delete. niceboy1419 14 — 4y
0
You would need to put an if statement, to see how many players there are. zandefear4 90 — 4y

2 answers

Log in to vote
0
Answered by 4 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:

--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.

Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago
--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.

Answer this question