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
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 = {}
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
5 comments
Your for second loops does not have an end. You should also consider cleaning up your code, that is one reason why I think you couldn't debug it yourself. If you want, I could post a slightly different version of your script.
This is a snippet of the code. I'll fix it.
Give me a few minutes to figure this out.
I'm working on an answer
Oh boy this is a lot