Balanced team select

It does not break, it just returns the first one of them. It’s up to you what you want to do next, you can look for other teams that have the same amount of players as the returned team.

I only wanted to inform @infiniteRaymond that there is an easier way to look for minimum instead of million if statements.

a ,figured it out

local teams = {game.Teams["Purple Kingdom"], game.Teams["Blue Kingdom"], game.Teams["Red Kingdom"], game.Teams["Green Kingdom"]} -- put all teams into that table
local minvalue = math.huge
local minindex = -1

	for i, team in ipairs(teams) do
		local count = #team:GetPlayers()
		if count < minvalue then
			minvalue = count
			minindex = i
			
			if minvalue == #teams[1]:GetPlayers() then -- Purple Kingdom with least
				print("a")
			end
			
			if minvalue == #teams[2]:GetPlayers() then -- Blue Kingdom with least
				print("b")
			end
			
			if minvalue == #teams[3]:GetPlayers() then -- Red Kingdom with leadt
				print("c")
			end
			
			if minvalue == #teams[4]:GetPlayers() then -- Green kingdom with least
				print("d")
			end
		end
	end

There is no point of putting these if statements into the loop. The goal of the loop was to evaluate the minimum, now you can put these ifs after the loop or even there is an easier and cleaner way to do that instead.

for i, team in ipairs(teams) do
    if minvalue == #team:GetPlayers() then
        print(team.Name)
    end
end
2 Likes

Just for some clarification, will this work when the player leaves?

while true do
	wait()
	
	Frame.JoinPurple.Text = "No"
	Frame.JoinBlue.Text = "No"
	Frame.JoinRed.Text = "No"
	Frame.JoinGreen.Text = "No"
	
	
	for i, team in ipairs(teams) do
		local count = #team:GetPlayers()
		if count < minvalue then
			minvalue = count
			minindex = i
			print(minvalue)
		end
		
		for i, team in ipairs(teams) do
			if minvalue < #teams[1]:GetPlayers() and minvalue < #teams[2]:GetPlayers() and minvalue < #teams[3]:GetPlayers() and minvalue < #teams[4]:GetPlayers() then
				minvalue = #teams[1]:GetPlayers()
				print(minvalue)
			end
			
			if minvalue == #team:GetPlayers() then
				if team.Name == "Purple Kingdom" then
					print("lol")
				end
			end
		end
	end
	-- end of loop 1
end
``

What are you trying to achieve here, because this code doesn’t make any sense.

I am trying to achieve a team system like arsenal.

When a player joins a team the Can Join is set to NO until the teams balance out.

Ex: red: 1 blue:1 purple:1 green:1

When a player leaves that team will be set to YES until the teams balance out.

1 Like