attempt to index global 'GAMEMODE' (a nil value)

Hi, I have a simple script i am running which is in my addons folder.
When the game loads i get the following script error attempt to index global ‘GAMEMODE’ (a nil value)

Here is my code, I dont understand what is wrong? as everyone saying changing GM to Gamemode will work:

local HideHudElements = {
    // HL2 HUD - set these up how you want...
    CAchievementNotificationPanel   = true; -- Achievement notifications
    CHudAmmo                        = true; -- Primary Ammo
    CHudBattery                     = true; -- Your Armor
    CHudChat                        = false; -- The chat area
    CHudCloseCaption                = false; -- The closed-caption messages
    CHudCredits                     = true; -- Credits
    CHudCrosshair                   = true; -- Your crosshair
    CHudDeathNotice                 = true; -- Death notices that appear at the top right of the screen
    CHudHealth                      = true; -- Your Health
    CHudHintDisplay                 = true; -- Displays hints as seen in Half-Life 2
    CHudHistoryResource             = true; -- List of items/ammunition the player recently picked up
    CHudSecondaryAmmo               = true; -- Secondary Ammo
    CHudSuitPower                   = false; -- Most likely Auxiliary Power
    CHudTrain                       = false; -- Controls when using a func_train?
    CHudMessage                     = false; -- Messages printed to the center of the screen
    CHudMenu                        = false; -- Unknown
    CHudWeapon                      = false; -- This is the indicators that appear when you have picked up ammo or weapons.
    CHudWeaponSelection             = false; -- Player weapon selection menu
    CHudGMod                        = false; -- HUD's produced by Garrysmod
    CHudDamageIndicator             = false; -- The red trapazoids displayed on the side of the screen when damage is taken, also includes the red screen you get when died from combat.
    CHudVehicle                     = false; -- Control panel when entering a vehicle/crane?
    CHudVoiceStatus                 = false; -- Shows when other players use microphone voice chat.
    CHudVoiceSelfStatus             = false; -- Shows when the local player uses microphone voice chat.
    CHudSquadStatus                 = false; -- Squad status panel shown when rebels join your squad
    CHudZoom                        = false; -- Dimming and large crosshair
    CHudCommentary                  = false; -- Display showing the duration and progress of the currently active commentary node
    CHudGeiger                      = false; -- Commentary panel
    CHudAnimationInfo               = false; -- Displays information about HUD elements, activated by the console command cl_animationinfo
    CHUDAutoAim                     = false; -- Unknown
    CHudFilmDemo                    = false; -- Unknown
    CHudHDRDemo                     = false; -- Lost Coast HDR Demonstration HUD element
    CHudPoisonDamageIndicator       = false; -- Panel that appears upon a player receiving poison [Neurotoxin] damage
    CPDumpPanel                     = false; -- Unknown

    // Acecool ShouldHideHud custom elements
    LuaHUDOurBasicHud               = false; -- Don't hide the basic hud we just added...


// Optimized HUDShouldDraw - super simple, running many thousands of times will only cost 0.08 seconds instead of over 15 seconds using table.HasValue
function GAMEMODE:HUDShouldDraw( _name )
    return !HideHudElements[ _name ];

hook.Add("HUDPaint", "LuaHUDOurBasicHud", function( )
    // Init vars
    local _p = LocalPlayer( );

    // Check if we should draw this hud
    local _shoulddraw = hook.Call( "HUDShouldDraw", GAMEMODE, "LuaHUDOurBasicHud" );

    // Mandatory Checks: If the player isn't valid, or we didn't want this to draw, then don't draw/return.
    if ( !IsValid( _p ) || !_shoulddraw ) then return; end

    // Mandatory Checks: Since we want to access the WEAPON data, we need to make sure the weapon is valid.
    // GetActiveWeapon will be a valid Player function, so we don't need to see if it exists since we know the player is valid at this point..
    local _w = _p:GetActiveWeapon( );
    if ( !IsValid( _w ) ) then return; end

    // Mandary Checks: Another important aspect, is a weapon isn't always a SWEP, so if you try to call "Ammo1" or
    // other SWEP function on a default HL2 weapon, it'll spam errors. I set this below the IsValid to give more
    // room to define WHY we do this check... Clip1 is a SWEP function, we can use ONE function to check for all SWEPS
    // unless you're using a custom base and only want this hud to show for that base ( but there is SWEP:HUD... function so.. )
    if ( !_w.Clip1 ) then return; end

    // Optional Checks: if player is spectator then don't draw/return ( Spectators wouldn't have a weapon, most likely... )...
    if ( _p:Team( ) == TEAM_SPECTATOR ) then return; end

    // Optional Checks: If this HUD

    // Do HUD stuff...

end );

Use hook.Run instead of hook.Call and remove the 2nd arg (GAMEMODE) and change GAMEMODE:HUDShouldDraw to GM:HUDShouldDraw.

Did all above and still have error

When outside a function use GM, when inside a function use GAMEMODE.

To save yourself the hassle you can make a utility function:
[LUA] function gm() return GM or GAMEMODE or gamemode.Get() end[/LUA]

Use self when you’re inside of a gamemode function. GM is used during initialization and GAMEMODE after that, it doesn’t matter whether or not you’re inside or outside a gamemode function. Using self ensures you never have to worry about getting the name of the gamemode table global right.

Agreed, I was talking about other functions though. I don’t normally find myself in gamemode methods.

I believe GM is set while the gamemode folder is loading, then it’s unset. For use outside of the gamemode or at a time other than load time, use GAMEMODE.
However, I don’t believe GAMEMODE isn’t set until Initialize runs.

Use this instead of the three lines where you have “function GAMEMODE:HUDShouldDraw”:
[lua]hook.Add(“HUDShouldDraw”, “OverrideHUD”, function( _name )
return !HideHudElements[ _name ];

I asked this question a year ago (or more, not sure) - does anyone even know why it changes from GM to GAMEMODE - why not just keep it as GM, or start as GAMEMODE? Surely that would make things 100% less complicated.