So Im making a gui that you can choose your team in so when you first join the game you are set as neutral and you can use the gui to pick a team that you want to join but I want to make a script that runs when you choose a team and makes sure the teams are balanced if someone can explain how to do this I would appreciate it since im trying to learn lua scripting language thanks!
This is actually quite simple. To start off let's see how many players are on each team:
local police = game.Teams.Police local prisoner = game.Teams.Prisoner local numpolice = #police:GetPlayers() local numprisoner = #prisoner:GetPlayers()
The :GetPlayers() function gives us a table, a group of objects. When we put a # in front of a table, it will give us the number of objects in the table. In this case, our number of objects is the number of players on a team
Now, depending on how many players are on each team, let's see if our player can join a team.
game.Players.LocalPlayer.PlayerGui.ScreenGui.Teams.Police.MouseButton1Click:Connect(function() if numpolice <= numprisoner then -- We allow them to join the team elseif numpolice > numprisoner then -- There are too many players, you could possibly put a GUI here that says that team has too many players end end) game.Players.LocalPlayer.PlayerGui.ScreenGui.Teams.Prisoner.MouseButton1Click:Connect(function() if numprisoner <= numpolice then -- Allow them to join the team elseif numprisoner > numpolice then -- Again, the teams are too full, so we can tell them by displaying a GUI end end)
So to recap what we just did, we set up a function to connect when the TextButton, or the team join button, is clicked. Inside this function, set up some things to determine whether they can join a team or not. First, we asked the script to figure out this: Are the number of police less than or equal to the prisoners? If so, we can allow them to join the team. Otherwise, they can't join it. We did the same with the prisoners.
I hope this helped answer your question!