• Trouble with keybinding
    8 replies, posted
Hello, I'm having trouble using some keybindings. I mashed some stuff from the wiki and another topic for this. Code: function KeysPressed (P, key, ply) if input.IsKeyDown(30) == 1 then ply:conCommand("DropWeapon") end if input.IsKeyDown(93) == 1 then ply:conCommand("BuyMenuHuman") end end hook.Add( "KeyPress", "KeyPressedHook", KeysPressed ) Pressing T(30) does nothing, not even produce an error. Neither does F2(93). Any ideas? I really need this fixed so I can use the buy menu ( It doesn't seem to work right if you just use it in the console in the menu ).
Can only use IN_KEY enumerations with that hook. And they would return booleans, not integers anyways. :) [b][url=wiki.garrysmod.com/?title=Gamemode.KeyPress]Gamemode.KeyPress [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]
[QUOTE=Drew P. Richard;25475031]Can only use IN_KEY enumerations with that hook. And they would return booleans, not integers anyways. :) [b][url=wiki.garrysmod.com/?title=Gamemode.KeyPress]Gamemode.KeyPress [img_thumb]http://wiki.garrysmod.com/favicon.ico[/img_thumb][/url][/b][/QUOTE] Hmm I see what you mean, can you provide an example script for another method?
You could hook it to Think, it is clientside. [lua] function KeysPressed() NextKeyThink = NextKeyThink or CurTime(); if NextKeyThink > CurTime() then return; end; if input.IsKeyDown(30) then RunConsoleCommand("DropWeapon"); NextKeyThink = CurTime() + 1; return; end if input.IsKeyDown(93) then RunConsoleCommand("BuyMenuHuman"); NextKeyThink = CurTime() + 1; return; end end hook.Add("Think","CheckKeysPressed",KeysPressed); [/lua] I wasn't sure if you held it down if it would spam or not, so I just made it wait a second after each time it's down.
This works, but I'm having trouble with another aspect of my menus. Something is causing my global integers to be null ( I use them as currency ). Heres the error: [gamemodes\laststand\gamemode\init.lua:153] attempt to call method 'GetGlobalInt' (a nil value) Heres snippets from the menu and npc kill parts: MENU Ready = vgui.Create( "DFrame" ) Ready:SetPos( ScrW() / 2, ScrH() / 2 ) //Set the position. Half the screen height and half the screen width. This will result in being bottom right of the middle of the screen. Ready:SetSize( 400, 400 ) //The size, in pixels of the Frame Ready:SetTitle( "Select Items/Upgrades" ) Ready:SetVisible( true ) Ready:SetDraggable( true ) Ready:ShowCloseButton( true ) Ready:MakePopup( true ) ready1 = vgui.Create( "DButton", Ready ) // Define ready1 as a "DButton" with its parent the Ready frame we just created above. ready1:SetPos( 20, 160 ) //Set position, relative to the frame (If you didn't parent it, it would be relative to the screen ready1:SetSize( 140, 40 ) // How big it should be, again in pixels ready1:SetText( "Buy SMG1-6/25" ) ready1.DoClick = function() //ready1.doclick = function, we just defined it as a function if player:GetGlobalInt("killcounter") >= 5 then ply:Give( "weapon_SMG1" ) Player:GiveAmmo(25,"smg1") ply:SetGlobalInt("killcounter", ply:GetGlobalInt("killcounter") - 5) end end NPC adder function KillCounter( victim, killer, weapon ) --Sets up a new function called KillCounter killer:SetGlobalInt("killcounter", killer:GetGlobalInt("killcounter") + 1) --Adds 1 everytime an NPC is killed. end hook.Add("OnNPCKilled","KillCounter", KillCounter) Can you help me on these?
GetGlobalInt and SetGlobalInt are not metatable functions, they are global functions, serverside only I believe. Use SetNetworkedInt and GetNetworkedInt. killer:SetNetworkedInt("killcounter",killer:GetNetworkedInt("killcounter")+1)
I have done as you said, but it has only solved that error in the serverside OnNpcKilled( or w/e its called ) function. Is there a way to allow a variable in the cl_init too?
[lua] ready1.DoClick = function() //ready1.doclick = function, we just defined it as a function if player:GetGlobalInt("killcounter") >= 5 then ply:Give( "weapon_SMG1" ) Player:GiveAmmo(25,"smg1") ply:SetGlobalInt("killcounter", ply:GetGlobalInt("killcounter") - 5) end [/lua] All of those need to be LocalPlayer() You haven't declared any of those to be valid variables. [lua] ready1.DoClick = function() //ready1.doclick = function, we just defined it as a function if LocalPlayer():GetNetworkedInt("killcounter") >= 5 then //ply:Give( "weapon_SMG1" ) <-- this is a serverside function! //Player:GiveAmmo(25,"smg1") <--this is too! //ply:SetNetworkedInt("killcounter", LocalPlayer():GetNetworkedInt("killcounter") - 5) <-- don't set these clientside, they're meant to be set serverside so they are networked to everyone. end [/lua]
Why aren't you using the ENUMs provided to make this process easier, and make your code more readable?
Sorry, you need to Log In to post a reply to this thread.