|
| 28 Nov 2015 04:13 PM |
Hello, does anyone know any good methods to make a team scramble. -I need it for a one time thing, so it goes through this other piece of code then scrambles the team, not every time a player joins. -Team's even.
Any help would be gratefully appreciated.. :D |
|
|
| Report Abuse |
|
|
|
| 28 Nov 2015 04:15 PM |
- Move all players to one team - Run an auto-balance script that chooses random players to balance out the teams
It scrambles them and evens them out at the same time. |
|
|
| Report Abuse |
|
|
|
| 28 Nov 2015 04:21 PM |
This is the script my game uses to scramble teams. However, it uses a custom team system and some methods are not shown. The purpose of this code is to help you understand how the general system might work.
function TeamUtil:PickTeams() local players = game.Players:GetPlayers() local function shufflePlayers() for i = 1, #players do for p, player in pairs(players) do local s = math.random(1, #players) players[p] = players[s] players[s] = player end end end local function getSmallestTeams() local smallestTeams = {} local teams = {} for t, team in ipairs(self.Teams) do teams[t] = team:MemberCount() end local lowestValue = math.min(unpack(teams)) for t, team in ipairs(self.Teams) do if (team:MemberCount() == lowestValue) then table.insert(smallestTeams, team) end end return smallestTeams end local function getWeakestTeam() local smallestTeams = getSmallestTeams() return smallestTeams[math.random(1, #smallestTeams)] end shufflePlayers() for _, player in ipairs(players) do local team = getWeakestTeam() team:AddMember(player) end end
/e tips_fedora | Developer | RBXDev Member |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 28 Nov 2015 04:24 PM |
local function balanceTeams(players, teams, randomize) for key = 1, #players do local player = table.remove(players, randomize and math.random(#players) or 1); player.TeamColor = teams[key % teams + 1].TeamColor; end end |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
| |
|
|
| 28 Nov 2015 04:35 PM |
local function Scramble(players,teams) local players,teams=players and players or game.Players:GetPlayers(),teams and teams or game.Teams:GetChildren()--Not really necessary, just for default arguments. for i=1,#players do table.remove(players,math.random(#players)).TeamColor=teams[i%#teams+1].TeamColor end end
That should work, call it like this:
Scramble() |
|
|
| Report Abuse |
|
|
| |
|
|
| 28 Nov 2015 04:37 PM |
| Lol cnt I just realized how methods were nearly the exact same. Kinda surprises me though because the last time we posted on a thread about randomizing teams you criticized this method because of how it goes 2 3 4 1 instead of 1 2 3 4. (Assuming 4 teams of course) |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 28 Nov 2015 04:47 PM |
| I know and I still think that it's gross but it's shorter so I did it anyways. |
|
|
| Report Abuse |
|
|
| |
|
|
| 28 Nov 2015 04:58 PM |
@cnt
I am stealing your function, lol |
|
|
| Report Abuse |
|
|