attempt to index global 'vgui'

Hello Again !

I’m here because I’ve a strange error with vgui. I know, I must use it on clientside and I’m trying to do this, but there is one problem…

My files are like that:

“abrabra” is my addon folder

addons/abrabra/lua/autorun/cl_init.lua
addons/abrabra/lua/autorun/init.lua
addons/abrabra/lua/autorun/shared.lua

Into shared.lua :



----------Colors---------
local clrTable = {} //Color table
clrTable["Green"] = Color(20, 150, 20, 255)
clrTable["Blue"] = Color(25, 25, 170, 255)
clrTable["Red"] = Color(150, 20, 20, 255)
clrTable["Brown"] = Color(102, 34, 0, 255)
-------------------------
 
TEAM_1 = 1
TEAM_2 = 2
TEAM_3 = 3
TEAM_4 = 4
 
team.SetUp( TEAM_1, "Team 1", clrTable["Green"] ) 
team.SetUp( TEAM_2, "Team 2", clrTable["Blue"] ) 
team.SetUp( TEAM_3, "Team 3", clrTable["Red"] ) 
team.SetUp( TEAM_4, "Team 4", clrTable["Brown"] )


Into cl_init.lua :



include( "shared.lua" )

function TeamMenu(  ) -- Starting the function.
 
// all the buttons I'm about to create are just a simple way to explain everything. I would make a table and make buttons that way but look through some more tutorials about loops till you do that.
local TeamMenu = vgui.Create( "DFrame" ) -- Creating the Vgui.
TeamMenu:SetPos( ScrW() +250, ScrH() / 2 -200 ) -- Setting the position of the menu.
TeamMenu:SetSize( 260, 210 ) -- Setting the size of the menu.
TeamMenu:SetTitle( "My test team selection menu" ) -- The menu title.
TeamMenu:ShowCloseButton( false ) -- Want us to see the close button? No.
TeamMenu:SetVisible( true ) -- Want it visible?
TeamMenu:SetDraggable( false ) -- Setting it draggable?
TeamMenu:MakePopup( ) -- And now we'll make it popup
function TeamMenu:Paint() -- This is the funny part. Let's paint it.
	draw.RoundedBox( 8, 0, 0, self:GetWide(), self:GetTall(), Color( 0,0,0,200 ) ) -- This paints, and round's the corners etc.
end -- Now we ONLY end the painting function.
 
-- This is a part which I had to add for the fun sake.
 
if !TeamMenu.Open then --  If the menu is closed, then
	TeamMenu:MoveTo(ScrW() / 2 - 250,  ScrH() / 2 - 200, 1.6, 0,1) -- When you open it, it will slide trough the screen, not teleport.
end -- Ending the if statement
 
-- Button time.
local team_1 = vgui.Create( "DButton", TeamMenu ) -- Creating the vgui of the button.
team_1:SetPos( 5, 30 ) -- Setting the position.
team_1:SetSize( 250, 30 ) -- Setting the size
team_1:SetText( "Team 1" ) -- Setting the text of the button
 
team_1.Paint = function() -- The paint function
    surface.SetDrawColor( 0, 255, 0, 255 ) -- What color do You want to paint the button (R, B, G, A)
    surface.DrawRect( 0, 0, team_1:GetWide(), team_1:GetTall() ) -- Paint what cords
end -- Ending the painting
 
team_1.DoClick = function() --Make the player join team 1
	RunConsoleCommand( "TEAM_1" )
	TeamMenu:Close() -- Close the DFrame (TeamMenu)
end -- Ending the button.
 
-- Now, this will be going on for 3 other buttons.
local team_2 = vgui.Create( "DButton", TeamMenu )
team_2:SetPos( 5, 70 )
team_2:SetSize( 250, 30 )
team_2:SetText( "Team 2" )
 
team_2.Paint = function() -- The paint function
	surface.SetDrawColor( 0, 0, 255, 255 ) -- What color do You want to paint the button (R, B, G, A)
	surface.DrawRect( 0, 0, team_2:GetWide(), team_2:GetTall() ) -- Paint what cords (Used a function to figure that out)
end
 
team_2.DoClick = function() --Make the player join team 2
	RunConsoleCommand( "TEAM_2" )
	TeamMenu:Close()
end
 
local team_3 = vgui.Create( "DButton", TeamMenu )
team_3:SetPos( 5, 110 ) --Place it next to our previous one
team_3:SetSize( 250, 30 )
team_3:SetText( "Team 3" )
 
team_3.Paint = function() -- The paint function
	surface.SetDrawColor( 255, 0, 0, 255 ) -- What color do You want to paint the button (R, B, G, A)
	surface.DrawRect( 0, 0, team_3:GetWide(), team_3:GetTall() ) -- Paint what cords (Used a function to figure that out)
end
 
team_3.DoClick = function() --Make the player join team 3
	RunConsoleCommand( "TEAM_3" )
	TeamMenu:Close()
end
 
local team_4 = vgui.Create( "DButton", TeamMenu )
team_4:SetPos( 5, 150 ) --Place it next to our previous one
team_4:SetSize( 250, 30 )
team_4:SetText( "Team 4" )
 
team_4.Paint = function() -- The paint function
	surface.SetDrawColor( 102, 34, 0, 255 ) -- What color do You want to paint the button (R, B, G, A)
	surface.DrawRect( 0, 0, team_3:GetWide(), team_3:GetTall() ) -- Paint what cords (Used a function to figure that out)
end
 
team_4.DoClick = function() --Make the player join team 4
	RunConsoleCommand( "TEAM_4" )
	TeamMenu:Close()
end
 
-- Here we are, the close button. The last button for this, because this is used instead of ShowCloseButton( false )
local close_button = vgui.Create( "DButton", TeamMenu )
close_button:SetPos( 5, 185 )
close_button:SetSize( 250, 20 )
close_button:SetText( "Close this menu" )
 
close_button.Paint = function()
    draw.RoundedBox( 8, 0, 0, close_button:GetWide(), close_button:GetTall(), Color( 0,0,0,225 ) )
    surface.DrawRect( 0, 0, close_button:GetWide(), close_button:GetTall() )
end
 
close_button.DoClick = function()
    TeamMenu:Close()
end
 
 end -- Now we'll end the whole function.
concommand.Add("TeamMenu", TeamMenu) -- Adding the Console Command. So whenever you enter your gamemode, simply type TeamMenu in console.


Into init.lua :



AddCSLuaFile( "shared.lua" )
AddCSLuaFile( "cl_init.lua" )
 
// You can make a simple function using arguements to make this less messier but this would be the simplest way to explain what it does.	
function TEAM_1( ply ) -- Creating the function.
	ply:UnSpectate() -- As soon as the person joins the team, he get's Un-spectated
	ply:SetTeam( 1 ) -- We'll set him to team 1
	ply:Spawn() -- Let's spawn him.
end -- End the function
concommand.Add("TEAM_1", TEAM_1) -- Adding a concommand (Console Command) for the team.
 
 
function TEAM_2( ply )
	ply:UnSpectate()
	ply:SetTeam( 2 )
	ply:Spawn()
end
concommand.Add("TEAM_2", TEAM_2)
 
function TEAM_3( ply )
	ply:UnSpectate()
	ply:SetTeam( 3 )
	ply:Spawn()
end
concommand.Add("TEAM_3", TEAM_3)
 
function TEAM_4( ply )
	ply:UnSpectate()
	ply:SetTeam( 4 )
	ply:Spawn()
end
concommand.Add("TEAM_4", TEAM_4)


When I type “TeamMenu” in my Console, I have this error :



[ERROR] addons/abrabra/lua/autorun/cl_init.lua:6: attempt to index global 'vgui' (a nil value)
  1. unknown - addons/abrabra/lua/autorun/cl_init.lua:6
   2. unknown - lua/includes/modules/concommand.lua:69


Can you help me please ?

You are trying to use clentside library vgui on server. Add “if !SERVER then end” checks where neccessary. Or simply “if !SERVER then return end” on top of the file.

Also, you should name your files more uniquely in autorun, as some other script might have the same name.

I’ve added “if SERVER then return end” on top of the file cl_init.lua. Now it said


Unknown Command: 'TeamMenu'

I’d suggest tabbing functions too. With not tabbing the start of the function, it becomes confusing as to what belongs where.

Take a look at this: https://dl.dropboxusercontent.com/u/26074909/tutoring/_tutorial_quizzes/_coding_standards.lua.html

And some working systems I’ve written to see what I mean in terms of tabbing and the difference it makes for readability – note, I don’t add nearly this many comments in code I write for use; nearly everything or everything is commented in each example and is meant for beginners to teach what everything is doing. There should be a fine balance, each bit [ read as function/method/algorithm ] of code should be self-explanatory, or easily explained using one or two short sentences and shouldn’t require a comment for each line:

https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/anti_damage_for_specific_teams_system.lua.html

https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/anti_teamkill_system.lua.html

https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/basic_hunger_system.lua.html

https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/player_jumped_sh_hook.lua.html

https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/playerselectspawn_system.lua.html

https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/spectating_players_system.lua.html

https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/weapon_name_e_to_pick_up_hudpaint.lua.html

I dont understand why it’s doesn’t work. Someone has other idea please ?

You could try moving cl_init to autorun/client and init to autorun/server