vgui.Create Issue?

Hello everyone,
I’m currently trying to get my own little addon working and would appreciate some help from you.
First off, I already have some LUA experience… But I haven’t made much stuff with Garry’s Mod yet.
That means I am not 100% sure if the addon structure is correct since the Garry’s Mod wiki still needs some work.

My addon structure:



../addons/myAddonName/addon.txt
../addons/myAddonName/lua/autorun/eg_load.lua
../addons/myAddonName/lua/eg/client/cl_gui.lua
../addons/myAddonName/lua/eg/server/download.lua
../addons/myAddonName/materials/ -- Some materials I need for the GUI


Content of addon.txt:



"AddonInfo"

{
	"name"		"MyAddonName"
	"version"		"1.0"
	"up_date"		"1.0"
	"author_name"	"LordofWar"
	"info"			"MyAddonInfo"
}



Content of eg_load.lua:



eg = {}

if SERVER then
	AddCSLuaFile("autorun/eg_load.lua") -- This file
	AddCSLuaFile("eg/client/cl_gui.lua") -- The GUI creation stuff
	
	include("eg/server/download.lua") -- For some resources the client will have to download from the server
else
	include("eg/client/cl_gui.lua")
end


Content of cl_gui.lua:



eg.gui = {}

local function createWindow()
	local window = vgui.Create("DFrame")
	window:SetSize(800, 600)
	window:Center()
	window:SetTitle("Title of window")
	
	local label = vgui.Create("DLabel", window)
	label:SetPos(650, 32.5)
	label:SetText("Text of label")
	label:SetTextColor(Color(255, 255, 255))
	label:SizeToContents()
	
	local sheet = vgui.Create("DPropertySheet", window)
	sheet:SetPos(10, 10)
	sheet:SetSize(790, 560)
	sheet:Center()
	local X, Y = sheet:GetPos()
	sheet:SetPos(X, Y + 15)
	
	eg.gui.window = {}
	eg.gui.window.window = window
	eg.gui.window.label = label
	eg.gui.window.sheet = sheet
end

-- Some other code that is not important --

createWindow()


I’m getting the following error message when I am spawning on the server:



[ERROR] addons/eg/lua/eg/client/cl_gui.lua:5: attempt to index local 'window' (a nil value)


Edit:
I just checked the console log again, line by line, and saw that the client prints out the following line:



Warning: vgui.Create failed to create the VGUI component (DFrame)


I really don’t know why this happens… When I enable cheats using “sv_cheats 1” and load the script “manually” with “lua_openscript_cl autorun/eg_load.lua” everything works fine.
As I already said above I would appreciate any help!

Somehow the script is ran serverside, and because vgui library doesn’t exist on server, it returns nil.

No, that would make it error with “attempt to index nil value: vgui at line 4”

print(window) right after your “local window = vgui.create…” to see if it returns nil.

If it does, try with Frame instead of DFrame?

Call local window = vgui.Create(“DFrame”) outside of the createwindow hook at the top

he’s calling it too late so

window:SetSize(800, 600) gets called instead.

Edit: It try’s to create DFrame to early lol

This doesn’t make sense. It waits for vgui.Create to finish before moving on.

Line 5 is “window:SetSize(…)” at that’s what returns a nil error.

That can only mean that vgui.Create didn’t work.

Sorry for letting you guys wait so long for my response. (Driving school…)
I just went through the console log line by line again and saw the following line:
(Sorry, looks like I have overlooked that before)



Warning: vgui.Create failed to create the VGUI component (DFrame)


Do what I said and it’ll work.

You need to call createWindow() in a hook with a small timer:

[lua]
eg.gui = {}

local function createWindow()
timer.Simple(1,function()
local window = vgui.Create(“DFrame”)
window:SetSize(800, 600)
window:Center()
window:SetTitle(“Title of window”)

	local label = vgui.Create("DLabel", window)
	label:SetPos(650, 32.5)
	label:SetText("Text of label")
	label:SetTextColor(Color(255, 255, 255))
	label:SizeToContents()
	
	local sheet = vgui.Create("DPropertySheet", window)
	sheet:SetPos(10, 10)
	sheet:SetSize(790, 560)
	sheet:Center()
	local X, Y = sheet:GetPos()
	sheet:SetPos(X, Y + 15)
	
	eg.gui.window = {}
	eg.gui.window.window = window
	eg.gui.window.label = label
	eg.gui.window.sheet = sheet
end)

end
hook.Add(“PlayerAuthed”, “eg_gui”, createWindow())
[/lua]

His code is fine.
[lua]eg.gui = {}

local window

local function createWindow()
timer.Simple(1,function()
window = vgui.Create(“DFrame”)
window:SetSize(800, 600)
window:Center()
window:SetTitle(“Title of window”)

local label = vgui.Create("DLabel", window)
label:SetPos(650, 32.5)
label:SetText("Text of label")
label:SetTextColor(Color(255, 255, 255))
label:SizeToContents()

local sheet = vgui.Create("DPropertySheet", window)
sheet:SetPos(10, 10)
sheet:SetSize(790, 560)
sheet:Center()
local X, Y = sheet:GetPos()
sheet:SetPos(X, Y + 15)

eg.gui.window = {}
eg.gui.window.window = window
eg.gui.window.label = label
eg.gui.window.sheet = sheet
 end)

end

– Some other code that is not important –

timer.Simple(2, function() createWindow() end)[/lua]

If he wants it in a playerspawn or w/e hook he’ll add it him self let him learn and stop doing everything for him.

VGUI isn’t valid so you need to delay it

You removed your whole message and edited into a new one?
Way to go.

Edit:
Rating me dumb doesn’t make it any better. :confused:

Did a test and worked fine



Panel: [name:EditablePanel][class:LuaEditablePanel][0,0,64,24]


Thanks for your help so far!

Sorry, but that hasn’t made the trick.

With defining window outside the function I get the following message:



[ERROR] addons/eg/lua/eg/client/cl_gui.lua:5: attempt to index upvalue 'window' (a nil value)


I will try out P.Tigris’s method with the timer now.

I edited it, After looking at these people code it’s now made me dumber from Wizard of ass post example.

That’s it! It worked!
I just removed the “PlayerAuthed” hook and placed your timer on the bottom of the file:



eg.gui = {}

local function createWindow()
	local window = vgui.Create("DFrame")
	window:SetSize(800, 600)
	window:Center()
	window:SetTitle("Title of window")
	
	local label = vgui.Create("DLabel", window)
	label:SetPos(650, 32.5)
	label:SetText("Text of label")
	label:SetTextColor(Color(255, 255, 255))
	label:SizeToContents()
	
	local sheet = vgui.Create("DPropertySheet", window)
	sheet:SetPos(10, 10)
	sheet:SetSize(790, 560)
	sheet:Center()
	local X, Y = sheet:GetPos()
	sheet:SetPos(X, Y + 15)
	
	eg.gui.window = {}
	eg.gui.window.window = window
	eg.gui.window.label = label
	eg.gui.window.sheet = sheet
end

-- Some other code that is not important --

timer.Simple(1,
	function()
		createWindow()
	end
)

Many thanks for the solution!

Your welcome, Call it in PlayerSpawn or PlayerInitialSpawn so it doesn’t error vgui.Create(“DFrame”) do it when the player is valid but if you only want to call it once do it what you’re doing now.

You’re welcome.

I hope you know that PlayerSpawn and PlayerInitialSpawn are serverside hooks…

Yes I know, anyway fix my reconnect.lua script saves me time. :v: