This is probably a simple fix and i’m just not doing something right. The script balance teams out and assigns the player to a team
Error:
ServerScriptService.RoundScript:87: attempt to index nil with 'GetChildren'
Line #87:
if #team:GetPlayers() > targetPlayersPerTeam then
Code:
local function balanceTeams()
local numTeams = #game.Teams:GetTeams()
local targetPlayersPerTeam = math.floor(#playingPlayers:GetChildren() / numTeams)
table.sort(playingPlayers:GetChildren(), function(a, b)
return #a:GetTeams() < #b:GetTeams()
end)
for i, player in ipairs(game.Players:GetChildren()) do
if player.Character.Parent == playingPlayers then
local team = player.Team
if #team:GetPlayers() > targetPlayersPerTeam then
local targetTeam
for _, otherTeam in ipairs(game.Teams:GetTeams()) do
if targetTeam == nil or #otherTeam:GetPlayers() < #targetTeam:GetPlayers() then
targetTeam = otherTeam
end
end
player.Team = targetTeam
end
end
end
end
Yeah I think your problem is, that you are using GetChildren on a number. You have to first declare GetChildren and then use # on it to get its number. So that would be my guess. Try:
local gamePlayers = workspace.GamePlayers
local playingPlayers = gamePlayers.PlayingPlayers:GetChildren()
local function balanceTeams()
local numTeams = #game.Teams:GetTeams()
local targetPlayersPerTeam = math.floor(#playingPlayers / numTeams)
table.sort(playingPlayers:GetChildren(), function(a, b)
return #a:GetTeams() < #b:GetTeams()
end)
for i, player in ipairs(game.Players:GetChildren()) do
if player.Character.Parent == playingPlayers then
local team = player.Team
if #team:GetPlayers() > targetPlayersPerTeam then
local targetTeam
for _, otherTeam in ipairs(game.Teams:GetTeams()) do
if targetTeam == nil or #otherTeam:GetPlayers() < #targetTeam:GetPlayers() then
targetTeam = otherTeam
end
end
player.Team = targetTeam
end
end
end
end
local function balanceTeams()
for _, player in pairs(game.Players:GetChildren()) do
if player.Character.Parent == playingPlayers then
local orangeTeam = game.Teams["OrangeTeam"]
local blueTeam = game.Teams["BlueTeam"]
local orangeCount = orangeTeam:GetPlayers()
local blueCount = blueTeam:GetPlayers()
if #orangeCount < #blueCount then
player.Team = orangeTeam
elseif #orangeCount > #blueCount then
player.Team = blueTeam
end
end
end
end
but the same problem is happing, no errors but player is not being assigned a team
Well because playingPlayers is now a table, since I used now :GetChildren() on it before. If you only want :GetChildren() in that one line, then:
local gamePlayers = workspace.GamePlayers
local playingPlayers = gamePlayers.PlayingPlayers
local playingPlayersTable = playingPlayers:GetChildren()
local function balanceTeams()
local numTeams = #game.Teams:GetTeams()
local targetPlayersPerTeam = math.floor(#playingPlayersTable / numTeams)
table.sort(playingPlayersTable, function(a, b)
return #a:GetTeams() < #b:GetTeams()
end)
for i, player in ipairs(game.Players:GetChildren()) do
if player.Character.Parent == playingPlayers then
local team = player.Team
if #team:GetPlayers() > targetPlayersPerTeam then
local targetTeam
for _, otherTeam in ipairs(game.Teams:GetTeams()) do
if targetTeam == nil or #otherTeam:GetPlayers() < #targetTeam:GetPlayers() then
targetTeam = otherTeam
end
end
player.Team = targetTeam
end
end
end
end
the script you provited it had the same error as the orangial code.
local function balanceTeams()
for _, player in pairs(game.Players:GetChildren()) do
if player.Character then
local orangeTeam = game.Teams["OrangeTeam"]
local blueTeam = game.Teams["BlueTeam"]
local orangeCount = orangeTeam:GetPlayers()
local blueCount = blueTeam:GetPlayers()
for _, player in pairs(game.Players:GetChildren()) do
if player.Character then
if #orangeCount <= #blueCount then
player.Team = orangeTeam
else
player.Team = blueTeam
end
end
end
end
end
end