First, house-keeping.
- Tab your code correctly
- Use
:GetPlayers() instead of :GetChildren() when you want a list of players
- You can use
math.random(high) instead of math.random(1, high) (this will help shorten things up later)
The trouble you'll be having is that you are using players[index] = nil to remove a player from a list. This creates a "gap" and will cause you to miscalculate #players and lose track of all of the others:
local list = {2, 3, 5, 7, 11, 13}
list[4] = nil
print(#list)
Instead, you want to table.remove(players, index). This also conveniently returns the thing removed.
Building redPlayers will now look like this:
for i = 1, redPlayers do
local player = table.remove(players, math.random(#players))
table.insert(redTable, player)
if player.TeamColor ~= BrickColor.Gray() then
table.insert(redTable, player)
player.TeamColor = BrickColor.Red()
end
end
I am not sure why you add the player to redTable twice... I'm guessing you just want simply this:
for i = 1, redPlayers do
local player = table.remove(players, math.random(#players))
table.insert(redTable, player)
player.TeamColor = BrickColor.Red()
end
If you only want to move players from the Gray team to Red and Blue, then players should have been built like that in the first place.
The resulting script could look something like this:
local players = {}
for _, player in pairs(game.Players:GetPlayers()) do
if player.TeamColor == BrickColor.Gray() then
table.insert(players, player)
end
end
local redTable = {}
local blueTable = {}
local redPlayers = 4
local bluePlayers = 6
for i = 1, redPlayers do
local player = table.remove(players, math.random(#players))
table.insert(redTable, player)
player.TeamColor = BrickColor.Red()
end
for i = 1, bluePlayers do
local player = table.remove(players, math.random(#players))
table.insert(blueTable, player)
player.TeamColor = BrickColor.Blue()
end
The last thing to be wary of is that if there are fewer players than redPlayers + bluePlayers, you'll get an error. Also, if there's more, not everyone will be distributed to a team.
You might want to consider something like
local redPlayers = math.floor(1/2 + #players * 4/6)
local bluePlayers = #players - redPlayers
Semi-advanced
There's no such thing as "advanced" code. There is a such thing as complicated code.
Most complicated code is more complicated than it should be.
Most code can be very simple. If you compare this script to yours, which is simpler? (Which also happens to be the one that doesn't error/make mistakes?)