Hooking F1

As you all know, I’ve been having difficulties getting something to come up when F1 is pressed in the gamemode I’m making, and so I was wondering how do I make it so that a derma menu comes up when you press F1?

I’ve tried that, and it hasn’t worked when I’ve hooked functions. So that gives me the question, does it not work if the gamemode is derived from base?

Post code as you are clearly doing it wrong



 
October 2009
257 Posts 
    Well here's the code for the rest of the files if anyone can help me. If you can, please help me.

init.lua


Code:
AddCSLuaFile( "shared.lua" )
AddCSLuaFile( "cl_init.lua" )

include( 'shared.lua' )
include( 'cl_init.lua' )

moneyOOCStart = 2000
walletStart = 100
 

function beginSpawn( ply )
		moneyOOC = ply:GetPData("OMoney")
		Player:StripWeapons()
		if moneyOOC == nil then
			ply:SetPData("moneyOOC", moneyOOCStart)
			ply:SetPData("wallet", walletStart)
		else
		ply:SetOOCMoney( OMoney )
		end
end
		
		
meta = FindMetaTable("Player") --Get the meta table of player
 
function meta:AddOMoney(amount)
	current_cash = self:GetOMoney()
	self:SetOMoney( current_cash + amount )
end
 
function meta:SetWallet(total)
		self:SetNetworkedInt( "wallet", total )
		self:SaveWallet()
end
 
function meta:SetOMoney(amount)
	self:SetNetworkedInt( "OMoney", amount )
	self:SaveMoney()
end
 
function meta:SaveMoney()
	cash = self:GetOMoney()
	self:SetPData("OMoney", cash)
end
 
function meta:SaveOMoneyTXT()
	file.Write(gmod.GetGamemode().Name .."/Money/".. string.gsub(self:SteamID(), ":", "_") ..".txt", self:GetOMoneyString())
end
 
function meta:
 
function meta:SaveOOCMoney()
	OMoney = self:GetOMoney()

function meta:TakeMoney(amount)
   --Add money function here
   self:AddMoney(-amount)
end
 
function meta:GetOMoney()
	return self:GetNetworkedInt( "OMoney" )
end
 
hook.Add( "PlayerInitialSpawn", "playerInitialSpawn", beginSpawn )
hook.Add( "ShowHelp", "Finances", personalFinances )

concommand.Add( "saveProgress", SaveMoney )


init.lua ^



Code:
include( 'shared.lua' )

function personalFinances

financeOwnPanel = vgui.Create( "DFrame" )
financeOwnPanel:SetPos( 300, 300 )
financeOwnPanel:SetSize( 400, 400 )
financeOwnPanel:SetTitle( "Personal Finances" )
financeOwnPanel:SetVisible( true )
financeOwnPanel:SetDraggable( true )
financeOwnPanel:ShowCloseButton( true )
financeOwnPanel:MakePopup()

financeOwnSave = vgui.Create( "DButton" )
financeOwnSave:SetParent( financeOwnPanel )
financeOwnSave:SetPos( 30, 360 )
financeOwnSave:SetSize( 20, 30 )
financeOwnSave:SetText( "Save" )
financeOwnSave.DoClick = function 
	RunConsoleCommand( "saveProgress" )
end

end

concommand.Add( "personalFinance", personalFinance )
hook.Add( "ShowHelp", "Finances", personalFinances )


^ cl.init





Code:
GM.Name	=		"Like RP"
GM.Author = 	"General Pancakes"
GM.Email = 		""
GM.Website =	"www.pancakelandcentral.webs.com"

team.SetUp( 1, "Tier I Law Enforcement", Color( 0, 0, 255, 255 ) )
team.SetUp( 2, "Tier II Law Enforcement", Color( 200, 200, 200, 255 ) )
team.SetUp( 3, "Tier III Law Enforcement", Color( 0, 255, 0, 255 ) )
team.SetUp( 4, "Outlaw", Color( 0, 0, 0, 255 ) )
team.SetUp( 5, "EMS", Color ( 255, 0, 0, 255 ) )
team.SetUp( 6, "Fire Department", Color ( 255, 0, 0, 255 ) )
team.SetUp( 7, "Corporation", Color ( 0, 0, 0, 255 ) )


^shared.lua

These are the codes for the files, but I think I may know why it hasn’t been working. Are player concommands different from server commands? If so that may be the culprit.

Are hooks clientside?

I don’t think it’s supposed to be that way, but in either case it isn’t working with it on either side, and also I know about the function error in cl_init.lua

Found 1 error on cl_init.lua:
[lua]function personalFinances[/lua]
That is sooo wrong, it is:
[lua]function personalFinances()[/lua]

In init.lua do like this
[lua]
hook.Add(“ShowHelp”, “OpenOnF1”, function ShowMenu( ply )
ply:ConCommand( “personalFinance” )
end )
[/lua]
So when the client pressed the F1 button, he will run the command “personalFinance”. The consolecommand you have made in your cl_init.lua
And remove the hook you got in your cl_init, you dont need it.

You are trying to run a clientside function from the server.
This does not work. You have to send a usermessage to the player to tell them they’ve pressed the button.
Also a lot of your code is syntactically wrong.
[lua]if (SERVER) then
umsg.PoolString(“ShowHelp”);
function GM:ShowHelp(ply)
SendUserMessage(“ShowHelp”,ply);
end
else
usermessage.Hook(“ShowHelp”,function()
– Do whatever it was you wanted to do here
end);
end[/lua]

[lua]include( ‘cl_init.lua’ )[/lua]
Hey, you just included the client-side file on the server… Don’t do that

ShowSpare1 is f3.

[lua]hook.Add( “ShowHelp”, “Finances”, personalFinances )[/lua]
Do it on the server, not on both…

[editline]02:47PM[/editline]

Edit: ShowHelp, not ShowSpare1

[editline]02:49PM[/editline]

cl_init.lua:
[lua]include( ‘shared.lua’ )

function personalFinances()

financeOwnPanel = vgui.Create( “DFrame” )
financeOwnPanel:SetPos( 300, 300 )
financeOwnPanel:SetSize( 400, 400 )
financeOwnPanel:SetTitle( “Personal Finances” )
financeOwnPanel:SetVisible( true )
financeOwnPanel:SetDraggable( true )
financeOwnPanel:ShowCloseButton( true )
financeOwnPanel:MakePopup()

financeOwnSave = vgui.Create( “DButton” )
financeOwnSave:SetParent( financeOwnPanel )
financeOwnSave:SetPos( 30, 360 )
financeOwnSave:SetSize( 20, 30 )
financeOwnSave:SetText( “Save” )
financeOwnSave.DoClick = function
RunConsoleCommand( “saveProgress” )
end

end

concommand.Add( “personalFinance”, personalFinances )[/lua]

init.lua:
[lua]AddCSLuaFile( “shared.lua” )
AddCSLuaFile( “cl_init.lua” )

include( ‘shared.lua’ )

moneyOOCStart = 2000
walletStart = 100

function beginSpawn( ply )
moneyOOC = ply:GetPData(“OMoney”)
Player:StripWeapons()
if moneyOOC == nil then
ply:SetPData(“moneyOOC”, moneyOOCStart)
ply:SetPData(“wallet”, walletStart)
else
ply:SetOOCMoney( OMoney )
end
end

meta = FindMetaTable(“Player”) --Get the meta table of player

function meta:AddOMoney(amount)
current_cash = self:GetOMoney()
self:SetOMoney( current_cash + amount )
end

function meta:SetWallet(total)
self:SetNetworkedInt( “wallet”, total )
self:SaveWallet()
end

function meta:SetOMoney(amount)
self:SetNetworkedInt( “OMoney”, amount )
self:SaveMoney()
end

function meta:SaveMoney()
cash = self:GetOMoney()
self:SetPData(“OMoney”, cash)
end

function meta:SaveOMoneyTXT()
file.Write(gmod.GetGamemode().Name …"/Money/"… string.gsub(self:SteamID(), “:”, “_”) …".txt", self:GetOMoneyString())
end

function meta:SaveOOCMoney()
OMoney = self:GetOMoney()
end

function meta:TakeMoney(amount)
–Add money function here
self:AddMoney(-amount)
end

function meta:GetOMoney()
return self:GetNetworkedInt( “OMoney” )
end

hook.Add( “PlayerInitialSpawn”, “playerInitialSpawn”, beginSpawn )
hook.Add( “ShowHelp”, “Finances”, function( ply )
ply:ConCommand( “personalFinance” )
end )

concommand.Add( “saveProgress”, SaveMoney )[/lua]

Did you actually try learning the basics of Lua before you started on this, or did you decide to pick it up as you went along?
You really can’t do that with Lua (or indeed most languages.)
You can either go for one of the badly written tutorials on the wiki or the Programming in Lua book which is not as brief, but far more comprehensive.

He seems to be not so bad at it… he only forgot a End and a ()… It’s mostly not knowing the way to do things / functions to use where and when.

Well guys, if nothing else thanks for helping me. I guess I’ve got a thing or two to fix when I get home. Right now I’m at school, researching lua and learning the ways of programming in gmod. So I’m hoping I’ll be able to get it working when I get home. Thanks again guys.

Why’re you using concommands? Usermessages are faster and neater.

I didn’t know that. I’m rather new to lua programming, but thanks for telling me this. If you wouldn’t mind, could you please point me in the direction of a tutorial or how to learn about this?

You, however, **are ** bad at it.

There’s no need to insult one another, let’s not start this shall we?