How would you make this open Once? Issue = Opens Alot of Team Screens instead of just 1.

That is the function where when you press F1 the Team Menu opens up. But instead of just 1 screen opening up, it opens up about 6. Anyone know why?


local function KeyPress()
	if input.IsKeyDown(KEY_F1) then
	RunConsoleCommand( "team_menu", set_team )
	end
end
hook.Add("Think","BM - Clients - Key",KeyPress)

The “Think” hook runs a lot of times per second, and you’re just checking if a certain key is held down and if it is, you run a console command. That’s why.

Well, I’ve looked for fixes for that. All what I’ve found is to use Think. Do you have any ideas what to do? Thanks for your help btw. <3

I don’t know if KEY_F1 variable is considered a number, but I would rather do it like this(Renamed a bit, also added some extras):


local function InputAction()
	if input.WasKeyReleased(KEY_F1) then
		RunConsoleCommand( "team_menu", set_team )
	end
	if input.WasKeyReleased(KEY_F2) then
		print("Hello World!")
	end
end
hook.Add("Think","InputAction",InputAction)

See I added the key_f2 as well for an example that you can read input from other keys and do multiple different actions. That is also why I named it InputAction.

Doesn’t seem to work. Very strange. :L

That wiki page should help you out

Whenever I need to do something like what you’re doing, I just have a variable in place to check if the key has already been pressed. Try this:

[lua]
local f1_down
local function KeyPress()

if not input.IsKeyDown( KEY_F1 ) then
	f1_down = false
	return
end

if f1_down then return end
f1_down = true

... do stuff ...

end

hook.Add( “Think”, “KeyPress”, KeyPress )
[/lua]

Couldn’t you just do this?



function teammenu( ply ) --Start the function
	ply:ConCommand( "team_menu" ) -- make the player execute the command
end --End the function
hook.Add("ShowHelp", "TeamMenuHook", teammenu )--Add the hook "ShowHelp" so it opens with F1


What if the player has their “ShowHelp” key bound to something other than F1? And regardless, with the method I mentioned he can easily use any key he wants that doesn’t rely on a hook to trigger it.