local EvilCount = 0 local GoodCount = 0 local NeutralCount = 0 repeat local index = math.random(1,#Evil) currentteam = Evil[index] print(currentteam.Name.." was chosen for this round.") currentteam.Parent = game.Teams table.insert(ChosenTeams, currentteam) table.remove(Evil, index) wait(1) EvilCount = EvilCount+1 until EvilCount == 2 repeat local index = math.random(1,#Good) currentteam = Good[index] print(currentteam.Name.." was chosen for this round.") currentteam.Parent = game.Teams table.insert(ChosenTeams, currentteam) table.remove(Good, index) wait(1) GoodCount = GoodCount+1 until GoodCount == 2 repeat local index = math.random(1,#Neutral) currentteam = Neutral[index] print(currentteam.Name.." was chosen for this round.") currentteam.Parent = game.Teams table.insert(ChosenTeams,currentteam) table.remove(Neutral, index) wait(1) NeutralCount = NeutralCount +1 until NeutralCount == 2 print(table.unpack(ChosenTeams)) for i, Team in pairs(ChosenTeams) do table.remove(ChosenTeams, i) table.insert(Teams, Team) wait(0.1) end print(table.unpack(ChosenTeams))
The issue lies on this line:
table.insert(Teams, 1, Team)
This line tells the script to:
Refer to the Teams table for insertion.
Assign index 1 to Team. If a value already exists at index 1, it will be overwritten.
The second bullet is where things are going wrong. Every time the for loop performs an iteration, it writes the Team value to the 1st index of the Teams table. Because it keeps doing this, the result is always going to be the 1st index of the Teams table being the last index of the ChosenTeams table, and the Teams table always having a length of 1.
If you tell table.insert()
to just insert the value without giving it a number, like so:
table.insert(Teams, Team)
The value Team
will go to the next empty index (so, for example, if index 1 contains a value, move to index 2 and insert a value into that index). This prevents any value in the table from being overwritten.
I have a team splitter that might help.. First off I think your over complicating stuff already.
local plrs = game.Players:GetPlayers() local Green = -- Change these and put your teams local Blue = local Red = for i,v in pairs(plrs) do if i > #plrs/2 then v.Team = Red else v.Team = Blue end end end
I have no idea if this fits your question.. but if it does then it might help.