Detecting player keypress to load derma menu

Messing around with a gamemode:

I want to create a menu that will pop up when a player presses a button (for now, “Q”).

Currently, this is on my cl_init.lua:

function mMenu()

    local ply = LocalPlayer()
    local BackGround = vgui.Create( "DFrame" )
    BackGround:SetSize( 200, 90 )
    BackGround:SetPos( (ScrW()/2)-BackGround:GetWide(), (ScrH()/2)-BackGround:GetTall() )
    BackGround:SetTitle( "Menu" )
    BackGround:SetVisible( true )
    BackGround:SetDraggable( true )
    BackGround:ShowCloseButton( true )
    local AButton = vgui.Create( "DButton", BackGround )
    AButton:SetSize( 160, 40 )
    AButton:SetText( "Button" )
    AButton:SetPos( 20, 30 )

function KeyPressed (ply, key)
    if (key == 1024)
    Msg (ply:GetName().." pressed "..key.."
hook.Add( "KeyPress", "KeyPressedHook", KeyPressed )

The menu will pop up when the player presses “d”, as that is key “1024” apparently. However I can’t seem to find the key code for “q”. Looking through the gmod wiki, it seems like “KeyPressed” may not be the right function to use. Maybe “Player.KeyDown”? But I have no idea how to implement that. Before I start going down the wrong road, could someone give some input?


Please use wiki next time.

Also a example if you want it to be like the spawn menu.

[LUA]local Models = {

local frame = vgui.Create(“DFrame”)
local IconList = vgui.Create( “DPanelList”, frame )

frame:SetPos((ScrW() / 2) - 175, (ScrH() / 2) - 200)
frame:SetTitle(“Spawn Menu”)

IconList:EnableVerticalScrollbar( true )
IconList:EnableHorizontal( true )
IconList:SetPadding( 4 )
IconList:SetSize(330, 360)

for k,v in pairs(Models) do
local icon = vgui.Create( “SpawnIcon”, IconList )
icon:SetModel( v )
IconList:AddItem( icon )
icon.DoClick = function( icon ) surface.PlaySound( “ui/buttonclickrelease.wav” ) RunConsoleCommand(“gm_spawn”, v) end
function GM:OnSpawnMenuOpen()
function GM:OnSpawnMenuClose()

I did. But as this is not Sandbox nor am I looking to create a spawn menu, I didn’t think to search for such things.
Thanks, though

i see.
no problem

When i said like the spawn menu i ment it to be you need to hold down Q to have it open.

That works great. However I’ve run into another roadblock. I want to run functions that need to be called by the server (Ex: SetMaxHealth() ). Is there a way to call a server function (in init.lua) from the client (cl_init.lua)?

Since shared is included in cl_init, can I put the SetMaxHealth in shared and reference shared.lua from cl_init.lua? I know this is getting off topic now, but I hate making a new topic for every question.

Basically, when I press a button in the derma frame defined in cl_init, I want to be able to change the max health (and other things, but if I can understand this, I can do the rest I think)

Usermessages :eng101:

To send data from cl_init to be used in init.lua

No concommand is only way for client->server transfer :pseudo:

Ok, maybe we take a different approach. Maybe I setup a variable on the client that the server continually looks for.

So the server is constantly updating MaxHealth by getting that variable from the player.

Is that possible? Seems like it would bog down the server though…

I’m attempting concommand now…


By using SetVar, GetVar, and concommand, I am able to accomplish what I want. By pressing a button in the frame, it executes a concommand which is picked up by shared. Shared checks the variable and executes a server command if appropriate.

The only issue I have right now is I can’t reference the player in cl_init. I tried saying, for example, ply = LocalPlayer() but it didn’t seem to work. Any thoughts? The idea is using GetVar in cl_init to check variables…

After using ply = LocalPlayer(), this is what I get in the console for an error:
gamemodes est\gamemode\cl_init.lua:56: attempt to index global ‘ply’ (a nil value)

This is line 56:
stat1:SetText( ply:GetVar(“points”) )

Starting a new topic as this has gotten far from the original post. Thanks for the help!

Do people really have to start this every time someone says the word usermessages or the word concommand?