attempt to index global 'GM' (a nil value) What the hell?

Whenever I use either GM or GAMEMODE, it tells me it is nil. I have no idea why it’s telling me this, it was working fine before.

The gamemode in question’s init.lua:


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

include( "shared.lua" )

function GM:Think()

	self.BaseClass:Think()
	if GM:CheckForRule("movespeed", true) then
		for k, v in pairs(player.GetAll()) do
   			GM:SetPlayerSpeed( v, 350, 700)
		end
	end
	if GM:CheckForRule("movespeed", false) then
		for k, v in pairs(player.GetAll()) do
   			GM:SetPlayerSpeed( v, 150, 300)
		end
	end
end

The gamemode’s shared.lua:


DeriveGamemode( "fretta" )

IncludePlayerClasses()	

GM.Name		= **************
GM.Author	= ************
GM.Email	= ""
GM.Website	= ""
GM.Help 	= ********************************
GM.TeamBased = true
GM.RoundBased = true
GM.RoundLimit = -1

GM.GameLength 			= 15	
GM.MaximumDeathLength	= 10
GM.MinimumDeathLength 	= 5
GM.RealisticFallDamage	= true
GM.EnableFreezeCam		= true
GM.SelectColor			= false
GM.SelectModel 			= false
GM.SelectClass 			= false
GM.ShowTeamName 		= false
GM.NoPlayerTeamDamage 		= false
GM.FragLimit			= 100
GM.RoundEndsWhenOneTeamAlive	= true

GM.VAWeapon			= "nowep"
GM.VARule1			= "norules"
GM.VARule2			= "rorules"
GM.VARule3			= "norules"
GM.VAWeaponList 		= {}
GM.VARulesList			= {}
GM.EnglishRules = {}

function GM:AddVARule( name, english )
	GM.EnglishRules[name] = english
	table.insert( GM.VARulesList, name )
end

function GM:AddVAWeapon( name )
	table.insert( GM.VAWeapomList, name )
end

GM:AddVARule("movespeedup", "Movement Speed Increase!")
GM:AddVARule("damageup", "Attack Strength Increase!")
GM:AddVARule("healthup", "Max Health Increase!")
GM:AddVARule("accuracyup", "Aimcone Decrease!")
GM:AddVARule("gunspeedup", "Rate of Fire Increase!")
GM:AddVARule("jumpup", "Higher Jump!")
GM:AddVARule("movespeeddn", "Movement Speed Decrease!")
GM:AddVARule("damagedn", "Attack Strength Decrease!")
GM:AddVARule("healthdn", "Max Health Decrease!")
GM:AddVARule("accuracydn", "Aimcone Increase!")
GM:AddVARule("gunspeeddn", "Rate of Fire Decrease!")
GM:AddVARule("jumpdn", "No jumping!")
GM:AddVARule("fragbullets", "Frag Rounds!")
GM:AddVARule("norules", "No Rules!")
GM:AddVARule("scopes", "All Weapons Have Scopes!")
GM:AddVARule("pushrounds", "Power Bullets!")
GM:AddVARule("vampire", "Vampire Rounds!")
GM:AddVARule("disease", "Deadly Disease!")
GM:AddVARule("hotgun", "Hotguns!")
GM:AddVARule("slugs", "Slugs!")
GM:AddVARule("rpgbullets", "Hit/Miss Chance!")

GM:AddVAWeapon("va_ak47")
GM:AddVAWeapon("va_awp")
GM:AddVAWeapon("va_deagle")

function GM:CreateTeams()

	team.SetUp( 1, "Administrators", Color( 70, 230, 70 ), true )
	team.SetSpawnPoint( 1, { "info_player_deathmatch", "info_player_counterterrorist" } )
	team.SetClass( 1, { "Admin" } )

	team.SetUp( 2, "Hackers", Color( 230, 70, 70 ), true )
	team.SetSpawnPoint( 2, { "info_player_deathmatch", "info_player_terrorist" } )
	team.SetClass( 2, { "Hax" } )

end

function GM:DetermineRandomWeapon()
	GM.VAWeapon = table.Random(GM.VAWeaponList)
	local weaponenglish = GM.VAWeapon
	weaponenglish = string.gsub(weaponenglish,"va_","")
	weaponenglish = string.upper(weaponenglish)
	PrintMessage( HUD_PRINTCENTER, "Round Weapon: "..weaponenglish )
	for k, v in pairs(player.GetAll()) do
   		v:Give(GM.VAWeapon)
		v:ConCommand("play variantassault/misc/rules_update.wav")
	end
end

function GM:DetermineRoundRules()
	// Rule number 1
	GM.VARule1 = table.Random(GM.VARulesList)
	// Rule number 2
	GM.VARule2 = table.Random(GM.VARulesList)
	// Rule number 3
	GM.VARule3 = table.Random(GM.VARulesList)
	
	local rulesline1 = GM:GetRule1English()
	local rulesline2 = GM:GetRule2English()
	local rulesline3 = GM:GetRule3English()
	local rulestr = "
"..GM:GetRule1English().."
"..GM:GetRule2English().."
"..GM:GetRule3English()
	PrintMessage( HUD_PRINTCENTER, "Round Rules:"..rulestr )
	for k, v in pairs(player.GetAll()) do
		v:ConCommand("play variantassault/misc/rules_update.wav")
	end
	if SERVER then
		umsg.Start( "RulesClientside" )
		umsg.String( GM:GetRule1English() )
		umsg.String( GM:GetRule2English() )
		umsg.String( GM:GetRule3English() )
		umsg.End()
	end
end

function GM:CheckForRule(rule)
	if GM.VARule1 == rule then return true end
	if GM.VARule2 == rule then return true end
	if GM.VARule3 == rule then return true end
	return false
end

function GM:GetRule1English()
	local retscript = GM.EnglishRules[GM.VARule1]
	return retscript
end
function GM:GetRule2English()
	local retscript = GM.EnglishRules[GM.VARule2]
	return retscript
end
function GM:GetRule3English()
	local retscript = GM.EnglishRules[GM.VARule3]
	return retscript
end

Result:


[@gamemodes\variantassault\gamemode\shared.lua:41] bad argument #1 to 'insert' (table expected, got nil)
[gamemodes\variantassault\gamemode\init.lua:11] attempt to index global 'GM' (a nil value)(Hook: Think)
[gamemodes\variantassault\gamemode\init.lua:11] attempt to index global 'GM' (a nil value)(Hook: Think)
[gamemodes\variantassault\gamemode\init.lua:11] attempt to index global 'GM' (a nil value)(Hook: Think)
[gamemodes\variantassault\gamemode\init.lua:11] attempt to index global 'GM' (a nil value)(Hook: Think)
etc etc etc, basically it spams in console

Additionally, gmod.GetGamemode() also returns nil.

use self instead of GM inside the function.

Some people that are new to lua get confused when they see this so I’m gonna explain it to you(to the op not to Wizard) in the most simple way so that you can understand it. Basicly when you call/create a function and it is something like Table:Function() then you are using meta tables. If you use “self|” inside the function, self will return the variable before the “:” in the function( the meta table ). Eample:

[lua]
local str = "Hello"f
unction str:Printme()
print(self)
end
[/lua]

Console putput will be “hello” because str is equal to “hello”. You should read this cause everything is explained better in there : Programming in Lua : 16 and Programming in Lua : 13 Hope this helps.

Or you could just say meta:Func( a ) = metaclass.Func( meta, a )
ie ply:Func( 2 ) = player.Func( ply, 2 )

Or just replace GM with GAMEMODE.