How do I fix this error?

I’m completely new to writing code for Gmod and I’m currently messing around with making a basic gamemode. I know that “ply” is just a variable name (atleast I’m sure it is…) but I’ve looked around on the Wiki and forums and I can’t figure out what I should be doing to make this work or how to declare “ply”.

The code is meant to allow players to join either team with the team “Nose” having a 1 player max etc. But I can’t get it to even load the init.lua.

This is the code:


AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")

include('shared.lua')


local TEAM_SPEC, TEAM_RUNNERS, TEAM_NOSE = 1, 2, 3

function GM:PlayerInitialSpawn(ply)
	if team.NumPlayers(TEAM_NOSE) >= 1 then
		self:SetTeam(2)
		self:SetModel("models/player/css_riot.mdl")
	elseif team.NumPlayers(TEAM_NOSE) < 1 then
		self:SetTeam(3)
		self:SetModel("models/player/css_pheonix.mdl")
		end
	end
	
	ply:SetWalkSpeed(250)
	ply:SetRunSpeed(320)
	ply:SetCrouchedWalkSpeed(0.3)
	ply:SetDuckSpeed(0.5)
	ply:SetNoCollideWithTeammates(true)

This is the error I’m getting.


[ERROR] gamemodes/shunt/gamemode/init.lua:19: attempt to index global 'ply' (a nil value)
  1. unknown - gamemodes/shunt/gamemode/init.lua:19


Any help, suggestions or advice is hugely appreciated.

What that error means is that ply is not defined as anything, because you ended the function that took ply as a parameter before you tried to access ply again. Try moving that second “end” to the bottom of the code and run it again.

Thanks, that resolved that error. Though now I’m being faced with a similar error:


[ERROR] gamemodes/shunt/gamemode/init.lua:14: attempt to call method 'SetTeam' (a nil value)
  1. unknown - gamemodes/shunt/gamemode/init.lua:14


EDIT:
Nevermind! I changed self to ply and that solved it. Thank you very much.