Very simple question abour GM:KeyPress

Can someone write a little piece of code for me using GM:KeyPress and tell me what it does?

The wiki has a short explaination. Basically when you press a key on the keyboard it runs this code, you get the key they pressed and who pressed it.

I ran this code but got an error, I don’t remember what it was, but this is exactly what I typed


function GM:KeyPress( Daniel41550, 34 )
	if ( key == IN_USE ) then
		print( "hi" )
	end
end

“Daniel41550” is not a valid player, You must use ply.

So ply means the player who called it? So I would just do


function GM:KeyPress( ply, 34 )
	if ( key == IN_USE ) then
		print( "hi" )
	end
end

34 is not a valid variable name ( they must start with a letter ), key is not defined.

You never define key.

edit: It took me 5 seconds to type this and I got ninja’d zzz

So I would put


function GM:KeyPress( ply, IN_JUMP )
	if ( key == IN_USE ) then
		print( "hi" )
	end
end

And whenever the player jumps, the code is run?

no because
if ( key == IN_USE ) then

I’d really recommend you read some things and get a stronger grasp of programming in general. You seem to be missing some base concepts.

[LUA]
function GM:KeyPress( ply, key)
if ( key == IN_USE ) then
print( “hi” )
end
end
[/LUA]

Yeah, you’re right. When I wanted to code in gmod, I never decided to actually learn, I just tried to look at code and teach myself. I’m gonna go watch some videos on lua programming.

Hey, at least you’re trying, rather than some people who just want everything handed to them.