Reason for Script: I am making a game that requires 3 different teams. One is Bright red, One is Bright blue, and one is Medium stone grey(Which is for lobby). For my game I am trying to put 4 players on one team(red team) and 6 on the other(blue team). What my script does so far is put 3 players on the Bright blue team(I tested this in an 8 Player Server).
-This is my script:
local players = game.Players:GetChildren() local redTable = {} local blueTable = {} local redPlayers = 4 local bluePlayers = 6 for i = 1, redPlayers do local index = math.random(1, #players) table.insert(redTable, players[index]) if players[index].TeamColor ~= BrickColor.Gray() then table.insert(redTable, players[index]) players[index].TeamColor = BrickColor.Red() players[index] = nil end end for i = 2, bluePlayers do local index = math.random(1, #players) if players[index].TeamColor ~= BrickColor.Blue() then table.insert(blueTable, players[index]) if players[index].TeamColor ~= BrickColor.Gray then table.insert(blueTable, players[index]) players[index].TeamColor = BrickColor.Blue() players[index] = nil end end end
First, house-keeping.
:GetPlayers()
instead of :GetChildren()
when you want a list of playersmath.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 -- list = {2, 3, 5, nil, 11, 13} print(#list) -- 3
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 -- players is only Gray-team players. 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
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?)