Attempt Get Team Players

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

As what is playingPlayers defined?

local gamePlayers = workspace.GamePlayers
local playingPlayers = gamePlayers.PlayingPlayers

If that was not what you where wanting the PlayingPlayers is a folder

It looks to me like you are attempting to call :GetPlayers() on a number, since putting “#” before an array will return a number.

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

There are no error that come out which is good but they player located inside of PlayingPlayers is not being assigned to a team

I remade it to be more simpler

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

Ok, I sovled it!!!

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

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.