Teams returning nil

So i’ve been jacking around with this gamemode for quite some time (2-3 months) and I’m pretty much ready to release. However I can simply not get rid of this one error.

[ERROR] gamemodes/ptp/gamemode/modules/base/sv_player.lua:224: attempt to index local 'teams' (a nil value)
1. PlayerSetModel - gamemodes/ptp/gamemode/modules/base/sv_player.lua:224
2. PlayerLoadout - gamemodes/ptp/gamemode/modules/base/sv_player.lua:214
3. unknown - gamemodes/ptp/gamemode/modules/base/sv_player.lua:72

Lines 221-234


Lines 214 and 72 are both just: GAMEMODE:PlayerLoadout( ply )

Anyone want to take a crack at it?

You want to insert the NewTeam table at the same index as the team index, or search GMTeams for the team table with the name of the team you are adding.

But wouldn’t I be searching GMTeams with the name of the team with ply:team() anyways?

Why would you search for it, if you can access it directly if you just use an index appropriate to what you are storing. If each team table is unique to a team, and there’s only one per team, then store it using the team index. Searching through the table, although in this instance wouldn’t be resource intensive, is a bad practice to learn.

Replace line 28 with:
GMTeams[ #GMTeams ] = NewTeam
Although I from first glance that might screw up indexing. You might want to ’ +1’ to ‘#GMTeams’ since lua array indexing starts at 1.

Still receiving this after following your instructions.

[ERROR] gamemodes/protectpresident/gamemode/modules/base/sv_player.lua:224: attempt to index local 'teams' (a nil value)
  1. PlayerSetModel - gamemodes/protectpresident/gamemode/modules/base/sv_player.lua:224
   2. PlayerLoadout - gamemodes/protectpresident/gamemode/modules/base/sv_player.lua:214
    3. unknown - gamemodes/protectpresident/gamemode/modules/base/sv_player.lua:72

The only thing that was changed is line 28 is now

GMTeams[ #GMTeams + 1 ] = NewTeam

Any other ideas?

[editline]26th May 2015[/editline]

Solution found. ply:SetTeam() was being called way too late and was trying to index TEAM_UNASSIGNED. Added a check to see if teams was nil and then called that function a bit later.