This script is supposed to equally randomize teams. It doesn't seem to be working.
--shuffle players team1=1 team2=0 for i,player in pairs(game.Players:GetPlayers()) do player.TeamColor=BrickColor.new("White") end players=game.Players:GetPlayers() for i=1,#players do repeat wait() n=math.random(1,#players) until players[n].TeamColor==BrickColor.new("White") if team1>team2 then team1=team1+2 players[n].TeamColor=mapclone.team1color.Value elseif team2>team1 then team2=team2+2 players[n].TeamColor=mapclone.team2color.Value end print(team1) print(team2) end
All it seems to do is put players on the same team. I tried server multiplayer test, same team. I could use quick help, I'm on a deadline. Thanks!
--shuffle players local team1 = 0 local team2 = 0 for i,player in pairs(game.Players:GetPlayers()) do player.TeamColor=BrickColor.new("White") end local players = game.Players:GetPlayers() for i = 1,#players do if team2 > team1 or team1 == team2 then --I switched around with them a little team1=team1+1 players[i].TeamColor= BrickColor.new("Really blue") elseif team1 > team2 then team2=team2+1 players[i].TeamColor= BrickColor.new("Really red") end print(team1 .. " team1") print(team2.. " team2") end
I tried this on studio test with 5 players and 3 people got placed in team 1 and 2 people in team 2.
I think i have the solution that you're looking for. To start though let me organize what we have. First everything that TheDeadlyPanther said wasn't wrong at all i will even use some of his code in this example. Secondly we know that we have a list of players and a set number of them where this list is returned in the same order provided players don't join or leave or an even number of players join or leave etc...
So how do we make this Random? We use the fisher-yates method. What this does is takes any list and randomizes all of its elements without creating or deleting any of them.
How it works :
lets say that we have a list of numbers 1 - 10 organized 1 - 10. start: 1 2 3 4 5 6 7 8 9 10
now we create a random argument from 1 - 10 for this example i will use 6. we then take the number at the 6th position out don't be confused here though we are NOT looking for the value 6 we want the value at index 6. we then place this in a new list as the first index. Next we take the last argument in the previous list in this case 10 and we make the randomly selected index 6 = the value at the last index 10.
old: 1 2 3 4 5 7 8 9 10 new: 6
we repeat this process until we have done this for all arguments in the first table.
now onto the code. So now that we know how to randomize a list we need to know how we can apply this to our situation of placing them on team 1 or 2. So im going to create 2 functions to handle this. the first one will return a player to give a team to and the 2rd one will decide what team to assign that player.
local team = 1 -- im going to use a flag variable instead of a counter function giveTeam(player) if team == 1 then player.TeamColor = mapclone.team1color.Value team = 2 else player.TeamColor = mapclone.team2color.Value team = 1 end end function shufflePlayers() pList = {} -- table to hold the players for i, v in pairs(game.Players:GetPlayers()) do pList[i] = v end for i = 1, #pList do local index = math.floor(math.random(#pList - i)) + 1 local value = pList[index] giveTeam(value) pList[index] = pList[#pList] table.remove(pList, #pList) end end
hope that this helps
--shuffle players local team1 = 0 local team2 = 0 for i,player in pairs(game.Players:GetPlayers()) do player.TeamColor=BrickColor.new("White") end local players = game.Players:GetPlayers() --local n = math.random(1,#players) for i = 1,#players do --repeat wait() n = math.random(1,#players) until players[n].TeamColor == BrickColor.new("White") if team1 > team2 or team1 == team2 then team1=team1+1 players[n].TeamColor=mapclone.team1color.Value elseif team2 > team1 then team2=team2+1 players[n].TeamColor=mapclone.team2color.Value end print(team1) print(team2) end
Hopefully this worked. I changed a few things, but it should work.
Some tips:
in pairs
for the second for
loop.Hope I helped :)
~TDP
team1 = mapclone.team1color.Value team2 = mapclone.team2color.Value for i,player in pairs(game.Players:GetPlayers()) do player.TeamColor=BrickColor.new('White') end for i, v in pairs(game.Players:GetPlayers()) do --for each player local currentPlayer = getTeam('White')[math.random(#getTeam('White'))] if #getTeam('White') <= (#getTeam(team1)-#getTeam(team2)) then --if there are barely enough players to make the teams even, make them even. currentPlayer.TeamColor = team2 elseif #getTeam('White') <= (#getTeam(team2)-#getTeam(team1)) then --same as before currentPlayer.TeamColor = team1 else local randomTeamNum = math.random() --get at random number if (randomTeamNum > (#getTeam(team1)/(#getTeam(team1)+#getTeam(team2)))) then --this bit does some fancy stuff --it makes it possible to be assigned either team --but makes it more likely to be assigned to the team with fewer players --based on the percentage of players between the two teams that are on the team. currentPlayer.TeamColor = team1 else currentPlayer.TeamColor = team2 end end end function getTeam(teamColor) --get the team or the number of players on the team local team = {} --make a table for the team players for i, v in pairs(game.Players:GetPlayers()) do --for each player if v.TeamColor == BrickColor.new(teamColor) then --if they're on team white table.insert(team, v); --put them in the table end end return team end
This does a bit of fancy chance stuff explained in the comments Have Fun!
I'll edit to add explanation or correct stuff if I need to.