Derma error/Open when spawn in for the firs time

When I run this I get this


lua: main.lua:3: attempt to index a nil value (global 'vgui')                                                                                                      


Here is the code:


local window
 
window = vgui.Create( "DFrame" )
window:SetTitle( "Choose A Faction!" ) 
window:SetSize( 300, 300 ) 
window:Center()
window:SetVisible( true ) 
window:ShowCloseButton( true ) 
window:SetDraggable( true ) 
window:MakePopup()
window.Paint = function( self, w, h ) 
	draw.RoundedBox( 0, 0, 0, w, h, Color( 0, 0, 0, 255 ) ) 
end

DermaImageButton = vgui.Create( "DImageButton", window )
DermaImageButton:SetPos( 50, 52 )								
DermaImageButton:SetSize( 200, 50 ) 
DermaImageButton:SetImage( "materials/us_icon.png" )		 
DermaImageButton:GetStretchToFit()							 
DermaImageButton.DoClick = function()
	RunConsoleCommand ( "say", "/usrecruit" )
end

DermaImageButton = vgui.Create( "DImageButton", window )
DermaImageButton:SetPos( 50, 113 )								
DermaImageButton:SetSize( 200, 50 )							
DermaImageButton:SetImage( "materials/ru_icon.png" )		 
DermaImageButton:GetStretchToFit()							
DermaImageButton.DoClick = function()
	RunConsoleCommand ( "say", "/rurecruit" )
end

DermaImageButton = vgui.Create( "DImageButton", window )
DermaImageButton:SetPos( 50, 173 )								
DermaImageButton:SetSize( 200, 50 )							
DermaImageButton:SetImage( "materials/uk_icon.png" )		 
DermaImageButton:GetStretchToFit()							
DermaImageButton.DoClick = function()
	RunConsoleCommand ( "say", "/ukrecruit" )
end

DermaImageButton = vgui.Create( "DImageButton", window )
DermaImageButton:SetPos( 50, 230 )								
DermaImageButton:SetSize( 200, 50 )							
DermaImageButton:SetImage( "materials/tali_icon.png" )		
DermaImageButton:GetStretchToFit()							 
DermaImageButton.DoClick = function()
	RunConsoleCommand ( "say", "/talirecruit" )
end

I also want to make it so that when a player joins this menu pop ups. BUT I am not sure where to put the code or what to put. If someone could put it in a response or maybe something really descriptive.