Frank-De-Ruiter Posted February 6, 2008 Share Posted February 6, 2008 Hi all, Ive already made an team balancer for 2 teams but i now need one for 4 teams, but i dont know how to make it like this. My 2 team balance script: function balanceTeams ( thePlayer ) local groveTeam = getTeamFromName ( "grove" ) local ballasTeam = getTeamFromName ( "ballas" ) local groveCount = countPlayersInTeam ( groveTeam ) local ballasCount = countPlayersInTeam ( ballasTeam ) if groveCount == ballasCount then setPlayerTeam ( thePlayer , groveTeam ) elseif groveCount > ballasCount then setPlayerTeam ( thePlayer , ballasTeam ) elseif groveCount < ballasCount then setPlayerTeam ( thePlayer , groveTeam ) end end mayby someone here on this forum knows the secret answer? Greatings DJFrankie, Thx For The Help! Link to comment
Tubbie Posted February 7, 2008 Share Posted February 7, 2008 Hmm I'm totally new to LUA and I've never created anything in LUA yet, so here's my try... don't know if it works and stuff, so please let me know how it goes. I want to start programming in LUA and make things for MTA as well, LOL... still lacking time tho Anywayz, I think the player should join the team with the least players... in that case you will always balance as good as possible. If teams are equal, the player just joins the first team. Hmmm, forgive me, I don't know what the team names are, so I just made up a few Whatever, here it goes: function balanceTeams(thePlayer) local teams = {getTeamFromName("grove"), getTeamFromName("ballas"), getTeamFromName("police"), getTeamFromName("gangster")} local pos = 1 local min = countPlayersInTeam(teams[pos]) for i = 2, teams.n do count = countPlayersInTeam(teams[i]) if count < c then min = count pos = i end end setPlayerTeam(thePlayer, teams[pos]) end Alternatively, if you want one count call less, you may store all counts in an array beforehand... function balanceTeams(thePlayer) local teams = {getTeamFromName("grove"), getTeamFromName("ballas"), getTeamFromName("police"), getTeamFromName("gangster")} local counts = {} for i = 1, teams.n do counts[i] = countPlayersInTeam(teams[i]) end local pos = 1 local min = counts[pos] for i = 2, teams.n do if counts[i] < c then min = counts[i] pos = i end end setPlayerTeam(thePlayer, teams[pos]) end Note this script is extendible to as much teams you like... just add some teams to the teams array et voilà Good luck and let me know how it goes! Oh by the way... WHY the HELL does LUA start counting at 1? I'm soooo used to Java LOL... LUA seems kinda weird to me, but I'll live Link to comment
Frank-De-Ruiter Posted February 8, 2008 Author Share Posted February 8, 2008 well this doesnt work for me, Something is worng in your script i think cant see it on the eye but it doesnt work so... Greatings Frank Link to comment
Tubbie Posted February 8, 2008 Share Posted February 8, 2008 Hmm what error messages do you get? Is it syntax? Cuz I'm not sure about the syntax LOL Perhaps my arrays are not correct or I use the same variable, i, which may not be valid... I don't know well, since you asked for it, I guess you have some more LUA experience.. ghehe By the way, did you check the team names, since I was not sure of them... oh and erm... if you change it to two teams, does it work? function balanceTeams(thePlayer) local teams = {getTeamFromName("grove"), getTeamFromName("ballas")} local counts = {} for i = 1, teams.n do counts[i] = countPlayersInTeam(teams[i]) end local pos = 1 local min = counts[pos] for i = 2, teams.n do if counts[i] < c then min = counts[i] pos = i end end setPlayerTeam(thePlayer, teams[pos]) end Link to comment
Tubbie Posted February 8, 2008 Share Posted February 8, 2008 Oh wait... I see the variable min is highlighted... so I think that's an operator in LUA, hmm.. why don't you try this: function balanceTeams(thePlayer) local teams = {getTeamFromName("grove"), getTeamFromName("ballas"), getTeamFromName("police"), getTeamFromName("gangster")} local counts = {} for i = 1, teams.n do counts[i] = countPlayersInTeam(teams[i]) end local pos = 1 local count = counts[pos] for i = 2, teams.n do if counts[i] < c then count = counts[i] pos = i end end setPlayerTeam(thePlayer, teams[pos]) end It should work what do you think about the algorithm? Does it make sense to you? Link to comment
Tubbie Posted February 12, 2008 Share Posted February 12, 2008 LOL Frank I see you've been active on the forums yesterday, but... I'm just curious... does my script work? Please let me know Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now