Hi guys. I just started learning lua and im curious about adding GUIs, so ive read a GUI toturial and tried to run it. I start a local multiplayer game as a listen server and have got my .lua file in my garrysmod/lua/autorun folder. But everytime i try to open the menu, i get this:
autorun/lern.lua:2: attempt to index global 'vgui' (a nil value)
Ive googled for it and the only thing i found was a thread here on the forums saying that my vgui libary isnt loaded. As im completely new to all of this, id need some help to understand what to do in order to fix this. Even when i try to load a very basic testframe code, i cant open it.
Any suggestions?
Thanks in advance, Hexo
garrysmod/lua/autorun/client
vgui needs to be created clientside.
Add
[lua]
if( SERVER ) then return end
[/lua]
Ontop of the lua file, this only allows client.
Thank you for not following the example of the people before you, and actually posting your entire error, from which I can guess your problem.
This is probably what your script looks like:
vgui.Create....
Not wrapped in any functions, or the function is being called right away. I see that you have the file in your autorun folder, and it is being run automatically right when lua initializes. You need to wait for vgui to be created before running it. So: put your vgui stuff in a function, then create a concommand to run it. Also, client side scripts go in autorun/client, not just autorun, and derma is solely client side.
[lua]
function makeDerma()
--put all your vgui stuff in this function
end
concommand.Add("dermaMenu", makeDerma)[/lua]
Then, when you type dermaMenu in the console, it will run your vgui stuff.
EDIT: Wow, double ninja'd. But still, vgui can't be created immediately on lua initialization, so do what I said too, as well as what those other two said.
Thanks for the quick replies, and, as i said i cant even run a testframe, i meant a testframe like its in the tutorials. So they actually are wrapped in a function.
this is taken from the tutorial [url]http://wiki.garrysmod.com/?title=Derma_VGUI_Example[/url]
[CODE]
function testpanel() -- Create the function
test = vgui.Create("DFrame") -- Create the frame
menu1 = vgui.Create("DButton") -- Create the button
test:SetPos(50,50) -- set the frame's position on the screen
test:SetSize(300, 300) -- set the frame size
test:SetTitle( "Test" ) -- set the frame title
test:SetVisible( true ) -- Make the frame visible
test:MakePopup() -- make the frame popup
menu1:SetParent( test ) -- parent the button to the frame
menu1:SetText( "Menu >" ) -- set the button text
menu1:SetPos(150, 150) -- set the button position in the frame
menu1:SetSize( 100, 20 ) -- set the button size
menu1.DoClick = function ( btn ) -- this will be called when the button is clicked
local menu123 = DermaMenu() -- create a derma menu
menu123:AddOption("hello", function() Msg("Hello") end ) -- adding options
menu123:AddOption("how", function() Msg("How") end )
menu123:AddOption("are", function() Msg("Are") end )
menu123:AddOption("you", function() Msg("You") end )
menu123:AddOption("?", function() Msg("?") end )
menu123:Open()
end -- ending the doclick function
end -- ending the function
concommand.Add("menutest", testpanel) -- adding the console command
[/CODE]
oh and, i had it in the autorun/client folder before, and it didnt work, so i asked someone else, and he told me to put it in the autorun folder. So what do i know? i can only listen to what people tell me when i got no clue of myself.
And thank you for beeing so ironic in your first sentence wich makes a nice welcome for newbies.
Did you add
[lua]if( SERVER ) then return end [/lua]
Above the function?
[QUOTE=Horsey;18166482]Did you add
[lua]if( SERVER ) then return end [/lua]
Above the function?[/QUOTE]
Just put it in the autorun/client folder. You don't need that.
I just tested the exact script you had in lua/autorun/client, and it works.
So did my notepad, but after a while it started reading the stuff as serverside.
By the way Yaka, the derma module gets loaded as soon as Lua initialises. Using it in an autorun script will never cause problems.
[QUOTE=| FlapJack |;18168614]By the way Yaka, the derma module gets loaded as soon as Lua initialises. Using it in an autorun script will never cause problems.[/QUOTE]
Oh, my mistake, there is such thing as vgui when the autorun scripts load, but you can't use it in an autorun script without wrapping it. For instance, this script:
[lua]local asdf = vgui.Create("DFrame")
local fefe = vgui.Create("DButton", asdf)[/lua]
when placed in autorun, gives these errors:
Warning: vgui.Create failed to create the VGUI component (DFrame)
Warning: vgui.Create failed to create the VGUI component (DButton)
I remembered there was an error if you tried to do that, I just got my errors mixed up.
Sorry, you need to Log In to post a reply to this thread.