Addon Problem

I’ve scripted a small command prompt as a substitute for console, just for learning and fun, and it works if I put it in to cl_init.lua of any gamemode, but it doesn’t work when I put it in to addon format.

It says this when I try to run the command.


autorun/prompt.lua:2: attempt to index global 'vgui' (a nil value)

Can anybody help me?

Here’s the code:
[lua]function CommandPrompt()
local CmdPrompt = vgui.Create( “DFrame” )
CmdPrompt:SetPos( 265,265 )
CmdPrompt:SetSize( 500, 50 )
CmdPrompt:SetTitle( “Command Prompt” )
CmdPrompt:ShowCloseButton( true )
CmdPrompt:SetVisible( true )
CmdPrompt:MakePopup()

local CmdText = vgui.Create( “DTextEntry”, CmdPrompt )
CmdText:SetPos( 20,25 )
CmdText:SetTall( 20 )
CmdText:SetWide( 450 )
CmdText:SetEnterAllowed( true )
CmdText:SetText( “Enter a command! Command Prompt by Jova.” )
CmdText.OnEnter = function()
RunConsoleCommand( unpack( string.Explode( " “, CmdText:GetValue() ) ) )
LocalPlayer():ChatPrint(LocalPlayer():Nick()…” Has ran ‘" …CmdText:GetValue()… "’." )
CmdPrompt:SetVisible( false )
end
end
concommand.Add( “prompt”, CommandPrompt )[/lua]

You need to put it into autorun/client or wrap it in an if CLIENT then … end statement.

I’ve tried that, It doesn’t work.

You have it in autorun/client ? or autorun/

EDIT: Ninja’d

I’ve tried if CLIENT then … end, and autorun/client.

[editline]05:21PM[/editline]

Doing this:
[lua]if CLIENT then
function CommandPrompt()
local CmdPrompt = vgui.Create( “DFrame” )
CmdPrompt:SetPos( 265,265 )
CmdPrompt:SetSize( 500, 50 )
CmdPrompt:SetTitle( “Command Prompt” )
CmdPrompt:ShowCloseButton( true )
CmdPrompt:SetVisible( true )
CmdPrompt:MakePopup()

local CmdText = vgui.Create( “DTextEntry”, CmdPrompt )
CmdText:SetPos( 20,25 )
CmdText:SetTall( 20 )
CmdText:SetWide( 450 )
CmdText:SetEnterAllowed( true )
CmdText:SetText( “Enter a command! Command Prompt by Jova.” )
CmdText.OnEnter = function()
RunConsoleCommand( unpack( string.Explode( " “, CmdText:GetValue() ) ) )
LocalPlayer():ChatPrint(LocalPlayer():Nick()…” Has ran ‘" …CmdText:GetValue()… "’." )
CmdPrompt:SetVisible( false )
end
end
concommand.Add( “prompt”, CommandPrompt )
end[/lua]

Just cancels the command, and says this


Unknown Command: 'prompt'

[editline]05:24PM[/editline]

And putting it in to a “client” folder does the same thing.


Unknown Command: 'prompt'

And the funny thing is, the first time I tested it, it worked perfectly fine, and did what it was supposed to do, but it stopped for some strange reason and I’ve talked to thomasfn and greasemonkey, and other scripters, but they don’t know how to fix it.

You’re putting the console command outside of the if statement, bad bad.

I’ve tried putting it inside. Doesn’t do shit :confused:

[editline]05:32PM[/editline]

Fixed that on the post.

Are you running a dedi? you may need to AddCSLuaFile it.

I will be soon. Can you tell me how to put it together to run both ways? (Client/Server === local/dedi)

[editline]06:09PM[/editline]

Fixed! :smiley: :smiley: :smiley:

Credits- iRzilla helped me fix it :smiley:

Releasing it in a few minutes.