Help with the creation of a Team Player Number Balancing Script/System

So I have two buttons that changes the teams of the player, only one time, at the start of the game, and it uses a remote event for activation:

This is located in the ServerScriptService, is a script:

Script
game.ReplicatedStorage.ChangeTeam.OnServerEvent:Connect (function(player, teamColor)
	player.TeamColor = teamColor
	player:LoadCharacter()
end)

This is located in a GUI, in a Frame, is a localscript:

Localscript
-- Vars
local RemoteEvent = game.ReplicatedStorage.ChangeTeam
local RedColor = "Bright red"
local BlueColor = "Bright blue"

local frame = script.Parent:WaitForChild("teamSelectionFrame")
local tutorialGui = game.Players.LocalPlayer.PlayerGui:WaitForChild("Tutorial")
local menuGui = game.Players.LocalPlayer.PlayerGui:WaitForChild("MENU")
local back = script.Parent.teamSelectionFrame.back

-- Script
frame["Red Bunnies"].MouseButton1Click:Connect(function()
	RemoteEvent:FireServer(BrickColor.new(RedColor))
	workspace.CurrentCamera.CameraType = "Custom"
	tutorialGui.Enabled = true
	game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed = 16
end)

frame["Blue Bunnies"].MouseButton1Click:Connect(function()
	RemoteEvent:FireServer(BrickColor.new(BlueColor))
	workspace.CurrentCamera.CameraType = "Custom"
	tutorialGui.Enabled = true
	game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed = 16
end)

back.MouseButton1Click:Connect(function()
	script.Parent.Enabled = false
	menuGui.Enabled = true
end)

It works perfectly but now I want to implement it so the player can’t access the other team if it would make the teams player number unbalanced. I’d appreaciate any suggestion, tip or help for this. If any other detail is needed, please let me know.

Order of the GUI and the buttons:
image

you could make two variables

local Blues = 0
local Reds = 0

and loop through everyplayer and check if either they are in team red or blue

for i,v in pairs(Players) do
    if v.Team.Name = "Bluez" then
    Blues += 1
    else
    Reds += 1
    end
end

and check if a team has way more than the other team

if Reds > Blues+2 then
   lock(Red)
   elseif Blues > Reds+2 then
   lock(Blue)
end

if so lock the team that has way more using
some kind of variable or something to lock the function for the team join
or just simply make the team join button that needs to be locked not visible

1 Like

I will try to put this into my code and see what comes up… If anyone else has suggestions, feel free to share them

frame["Red Bunnies"].MouseButton1Click:Connect(function()
        if #game.Teams["Red Bunnies"]:GetPlayers()-#game.Teams["Blue Bunnies"]:GetPlayers() >= 2 then return end
	RemoteEvent:FireServer(BrickColor.new(RedColor))
	workspace.CurrentCamera.CameraType = "Custom"
	tutorialGui.Enabled = true
	game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed = 16
end)

frame["Blue Bunnies"].MouseButton1Click:Connect(function()
        if #game.Teams["Blue Bunnies"]:GetPlayers()-#game.Teams["Red Bunnies"]:GetPlayers() >= 2 then return end
	RemoteEvent:FireServer(BrickColor.new(BlueColor))
	workspace.CurrentCamera.CameraType = "Custom"
	tutorialGui.Enabled = true
	game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed = 16
end)
2 Likes

Tried but it doesn’t seems to work…

I also tried this one but it doesn’t seems to work either. I’ll try to tweak things

@sleepcodes 's solution work. The tweak had to be done by my part on setting the player Neutral value to false to properly work.

sleepcodes solution:

3 Likes