Assign player function giving error

I got an error in my script for assigning a player to a random team:


Here is my code:

function TeamManager:AssignPlayerToTeam(player)
	local smallestTeam
	local lowestCount = math.huge
	for team, playerList in pairs(TeamPlayers) do
		if playerList < lowestCount then
			smallestTeam = team
			lowestCount = playerList
		end
	end
	table.insert(TeamPlayers[smallestTeam], player)
	player.Neutral = false
	player.TeamColor = smallestTeam.TeamColor
end

The line of code showing the error is here:

table.insert(TeamPlayers[smallestTeam], player)

What is the problem?
Some other essential code:

local TeamPlayers = {}
local TeamScores = {}

Seems like TeamPlayers[smallestTeam] is a number, not a table. Where in your code do you assign it?

3 Likes

Here, when the round is starting:

function GameManager.GameStart()
		GameManager.gameRunning = true
		print(tostring(GameManager.gameRunning))
		local mapsAvailable = savedMaps:GetChildren()
		mapsManager.LoadMap(mapsAvailable[math.random(1, #mapsAvailable)].Name)
		
		print("success")
		
		for i, plr in pairs(Players:GetPlayers()) do
			DisplayManager:ShowFrame(plr:WaitForChild("PlayerGui").Main, "ScoreboardTwoTeams")
			DisplayManager:HideFrame(plr:WaitForChild("PlayerGui").Main, "Intermission")
			teamManager:AssignPlayerToTeam(plr)
			plr:LoadCharacter()
			print('succesS.')
		end
end

Also more essential code:

for i, team in pairs(teamService:GetTeams()) do
	TeamPlayers[team] = {}
	TeamPlayers[team] = 0
end

Wait a second. I think I’ve figured out the problem. I did this:

	TeamPlayers[team] = {}
	TeamPlayers[team] = 0

It should be

	TeamPlayers[team] = {}
	TeamScores[team] = 0

EDIT: It works now lol

1 Like