Derma Basic Guide - Attempt to index global 'vgui' (a nil value)

Hello there. I’m new to Gmod Lua scripting, although not new to the Lua language itself. I’ve been going through the Lua tutorials on the official wiki, and I’ve become stuck on this one:

Specifically, I’m stuck on the DFrame. Since I’ve had quite a good run with the Chair Thrower tutorial (and even made some heavy and interesting modifications!) I was confident with this tutorial and tried to make a toggleable derma via concommand. It turned out not working, for reasons I’m sure have to do with my misunderstanding. So, I decided to just use the code they provided to find out how it worked, and run it by lua_openscript (only their code, I didn’t add anything or remove anything). It gave me this error:

[ERROR] lua/dermatest.lua:3: attempt to index global ‘vgui’ (a nil value)

  1. unknown - lua/dermatest.lua:3

I was a bit confused. I tried running it multiple times, in case vgui just took a while to initialize or something. Same error every time. Tried reloading, rechecked that it was the exact code the derma guide provided, and I still get it. So, then I tried instead to narrow down what the issue was, by checking from the beginning if vgui “exists”. From my tests, it seems to be telling me it “doesn’t exist”, and I can’t create the panel at all. The code I am using is the exact DFrame code provided on the guide I linked to above. Am I doing something wrong?

I am willing to provide as much information as I can regarding the problem. Thank you in advance for any help, and I hope it’s not something horribly dumb I overlooked. :downs:

The vgui library doesn’t exist serverside.

You’ll have to put “if SERVER then return end” at the top of your file.

Ah ha! That stopped the error… unfortunately, the DFrame still doesn’t show up upon running it. I’m not quite sure why… any ideas?

Easiest way I know of to constantly run and update vgui is to put it inside “lua/autorun/client” or “lua/autorun” with “if SERVER then return end” at the top.
local function MyMenu()
if IsValid(DMain) then DMain:Remove() end --Don’t spam yourself with panels

--When you're done with it make it local depending on how you're planning on using it
DMain:SetPos(5, 5)
DMain:SetSize(300, 150)
DMain:SetTitle("Name window")


I put the code inside lua/autorun/client, with ‘if SERVER then return end’ at the top. Now I have this error:

[ERROR] lua/autorun/client/dermatest.lua:9: attempt to index global ‘DMain’ (a nil value)

  1. MyMenu - lua/autorun/client/dermatest.lua:9
  2. unknown - lua/autorun/client/dermatest.lua:16

EDIT: The weird part is, sometimes when I save, the Lua auto-refresh allows the Derma to show properly. However, upon reloading, it goes back to showing the error. I’m confused.

That will happen when lua hasn’t loaded yet, just backspace something like the “)”, type ) then save it and it will refresh.

Or you can put,
timer.Simple(0, MyMenu)

Also, you don’t need the “if SERVER then return end” if it’s only loading clientside, which in your case it is. It’s only needed if you have it in autorun, which is shared, you have it in autorun/client which is client.

Your timer worked! Although I don’t understand that… when I was doing it by lua_openscript, Lua should have had plenty of time to load… I’ll have to do some more experimenting. Thanks for the help!

You don’t have to use, lua_openscript or lua_openscript_cl if your file is in the autorun directory, or if it’s a gamemode.

As of GM13 lua will refresh itself when you save the file.

The timer.Simple(0, MyMenu) is so that it will run the function on the next frame, since Timers work from the Think hook. So when the player first loads, it will take one frame to load clientside lua, AFAIK.

I suppose it’s worth asking out of curiousity before I mark this as solved… how do I properly use lua_openscript_cl? I am yet to find the directory that is utilized by that command, and can’t seem to run anything with it.

Why would you want to use that when lua already refreshes for you?

Just curious. I’ll mark this as solved now.