VGUI = "nil value"?

So i took a long break from Lua, went on to program in C#, then C and C++ a little.

Anyhow, i’m jumping back into the Lua scene. I’m trying to learn the ropes again and make a gamemode.

I copied a VGUI function that makes a DFrame. However, when the script is run i get an error or warning that “vgui” in “vgui.Create()” is a nil value.

Because i’ve been here before, i used Lua_Run in the console and ran my vgui function, and it painted the window normally.

I linked the function, which is run and linked to a ConCommand inside a file included by cl_init.lua, which is then run in GM:PlayerInitialSpawn (inside init.lua).

My question is, why does it see this as nil? Is it the concommand being run in init.lua? And if its nil as it says, why does it function correctly?


After a little modification, the panel is created within the cl_init file.

However, the error still remains.

Was there a memo i didn’t get?

It probably gets called twice - client side and server side. The error is thrown on the server.

If it helps, my code:



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

include( 'shared.lua' )
include( 'cl_init.lua' )

function GM:PlayerSpawn( ply )
    self.BaseClass:PlayerSpawn( ply )   
    ply:SetGravity( 0.75 )  
    ply:SetMaxHealth( 100, true )  
    ply:SetWalkSpeed( 325 )  
	 ply:SetRunSpeed( 325 ) 

function GM:PlayerInitialSpawn( ply )

function GM:PlayerLoadout( ply ) // What should the player recieve when joining a team?

	ply:Give( "weapon_physcannon" )



function TutWindow1()

   frame = vgui.Create( "DFrame" )
   frame:SetSize( 640, 480 )
   frame:SetTitle( "Testing Derma Stuff" )
   frame:SetVisible( true )
   frame:SetDraggable( false )
   frame:ShowCloseButton( false )


concommand.Add( "ShowTutorial", TutWindow1 )

function hidehud(name)
	for k, v in pairs{"CHudHealth", "CHudBattery", "CHudAmmo", "CHudSecondaryAmmo"} do
		if name == v then return false end
hook.Add("HUDShouldDraw", "hidehud", hidehud)

The error i recieve is:

gamemodes\attrrp\gamemode\cl_init.lua:6: attempt to index global ‘vgui’ (a nil value)


After throwing it into the cl_init file, it doesn’t run with the console command, it complains.

Doesn’t make sense, i can run it with the lua_run command, running the function directly, but it doesn’t like the ConCommand.

maybe the function is called too fast after the player spawn, just a guess.

You shouldn’t be able to run it with lua_run. Only with lua_run_cl.

You have a legal copy of Garry’s Mod, don’t you?

Yes, you are correct. Its being run with Lua_run_cl.

And yes i have a legal copy of Garry’s Mod. I left that scene a long time ago.

I figure this may be the cause, but it doesn’t run even after spawning.

throws me a nil value error when trying to run it from the ConCommand.


Is the error you’re getting blue or yellow?

Oh and welcome back to the scene, IceBreaker.

Aha, so the color of the error DOES mean something. I thought it just alternated between yellow and blue with no real meaning.

The error is blue.

And thanks, it’s good to be back.

Did nobody notice this
[lua]include( ‘cl_init.lua’ )[/lua]
in init.lua the OP posted?

Remove that line, you’re including a client-side file server-side.

Oh wow…

Heh. yep, been a while.

Thanks, raBBish.

Now the concommand says it doesn’t exist, but i think i may be able to figure this out on my own. If not you’ll see a thread.

Thanks for your help guys.