Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Table loop with players; works but then breaks. Any help? (Semi-Advanced)

Asked by 8 years ago

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

1 answer

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

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

-- 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

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?)

Ad

Answer this question