Hi, I was wondering if I could have suggestions to make a script for:
2 players max on home team if there is a person on team “GKH”
2 players max on away team if there is a person on team “GKA”
Current script (which just puts the player in a random team)
for _, Player in pairs(Players:GetChildren())do
if Player.Character and Player.Character:FindFirstChild('Humanoid') then
if Player.Team == Team.Choosing then
local randomTeam = Teams[math.random(1, #Teams)]
Player.Team = randomTeam
end
end
end
I was also wondering if there was a way to stop the randomTeam being “GKH” or “GKA”
You can add int values for each team. When the player joins the team, value increases, when he leaves, it decreases. And every time assigning the team you check the values and assign accordingly
You can do a lots of things. Easiest is adding int value for each team in explorer (4 teams, 4 values). That way team counts are accessible everywhere, and im sure 4 values wont affect performance. If anything they might even improve performance, since operations with them will be much simpler than with various tables, bindable events (to pass value through scripts) and etc.
for _, Player in pairs(Players:GetChildren())do
if Player.Team == Team.Choosing then
local randomTeam = Teams[math.random(1, #Teams)]
if HomeVal < 3 then
Player.Team = Home
HomeVal += 1
elseif AwayVal < 3 then
Player.Team = Away
AwayVal += 1
end
end
end
But I don’t think that will make the teams balanced if there are not 6 players in one server.
You could try checking which one has the lower players, and adding the player to that team. If this is not the intended behavior, could you please describe the desired one in a little more detail?
like @Bilon said, you d want to assign the team that has lower players, in this case you need random only when the teams are equal
for _, Player in pairs(Players:GetChildren())do
if Player.Team == Team.Choosing then
if HomeVal < AwayVal then
Player.Team = Home
HomeVal += 1
elseif HomeVal > AwayVal then
Player.Team = Away
AwayVal += 1
elseif HomeVal == AwayVal then
local randomTeam = Teams[math.random(1, #Teams)]
Player.Team = randomTeam
end
end
end
for _, Player in pairs(Players:GetChildren())do
if Player.Character and Player.Character:FindFirstChild('Humanoid') then
if Player.Team == Team.Choosing then
if HomeVal < AwayVal then
Player.Team = Home
HomeVal += 1
elseif HomeVal > AwayVal then
Player.Team = Away
AwayVal += 1
elseif HomeVal == AwayVal then
local randomTeam = math.random(1, 2)
if randomTeam == 1 then
Player.Team = 1
elseif randomTeam == 2 then
Player.Team = 2
end
end
end
end
end