• While keydown... (Sprinting)
    25 replies, posted
This is my current code [CODE] function KeyPressed (ply, key) if( ply:KeyDown( IN_FORWARD ) and ply:KeyDown( IN_SPEED )) then Msg("Running") end end hook.Add( "KeyPress", "KeyPressedHook", KeyPressed ) [/CODE]When I run (Pressing shift and W), it prints "Running" in the console. The problem is, I need it to be able to CONTINUOUSLY put "Running" in the console while the keys are being pressed. Lua doesn't seem to support while loops though.. Thoughts?
I am not 100% sure. Correct me if I am wrong, but your hook is being added after the pressing of the keys. Possibly try.. [lua] hook.Add( "KeyPress", "KeyPressedHook", KeyPressed function KeyPressed (ply, key) if( ply:KeyDown( IN_FORWARD ) and ply:KeyDown( IN_SPEED )) then Msg("Running") end end)[/lua]
Hmm... giving me errors about parentheses missing....
This should work. [lua] if input.IsKeyDown( IN_FORWARD ) and input.IsKeyDown( IN_SPEED ) then LocalPlayer():ChatPrint( "Running!" ) end[/lua]
[QUOTE=JTG2003;22594229]Hmm... giving me errors about parentheses missing....[/QUOTE] [lua] hook.Add( "KeyPress", "KeyPressedHook", KeyPressed, function KeyPressed (ply, key) if( ply:KeyDown( IN_FORWARD ) and ply:KeyDown( IN_SPEED )) then Msg("Running") end end) [/lua] Try now, I forgot the comma after KeyPressed. [QUOTE=Jova;22594277]This should work. [lua] if input.IsKeyDown( IN_FORWARD ) and input.IsKeyDown( IN_SPEED ) then LocalPlayer():ChatPrint( "Running!" ) end[/lua][/QUOTE] How would this work, it does the same thing as before just it is written different.
I say it's more sufficient. And it's just another possibility. [QUOTE=Extazy;22594302][lua] hook.Add( "KeyPress", "KeyPressedHook", KeyPressed, function KeyPressed (ply, key) if( ply:KeyDown( IN_FORWARD ) and ply:KeyDown( IN_SPEED )) then Msg("Running") end end) [/lua] [/QUOTE] This doesn't look like it will work. [lua] hook.Add( "KeyPress", "KeyPressedHook", function(ply, key) if( ply:KeyDown( IN_FORWARD ) and ply:KeyDown( IN_SPEED )) then Msg("Running") end end)[/lua]
[QUOTE=Jova;22594277]This should work. if input.IsKeyDown( IN_FORWARD ) and input.IsKeyDown( IN_SPEED ) then LocalPlayer():ChatPrint( "Running!" ) end[/QUOTE] "attempt to index global 'input' (a nil value)"
I don't know why input is a nil value. It shouldn't be.
[QUOTE=Extazy;22594302]hook.Add( "KeyPress", "KeyPressedHook", KeyPressed, function KeyPressed (ply, key) if( ply:KeyDown( IN_FORWARD ) and ply:KeyDown( IN_SPEED )) then Msg("Running") end end) Try now, I forgot the comma after KeyPressed. I added it in myself earlier, same error
I just tested my way, it didn't work. Im still new to lua, I was just taking a shot. So don't bother with my code. :ninja:
[QUOTE=Jova;22594398]I don't know why input is a nil value. It shouldn't be.[/QUOTE] Ah ha, that's a clientside function.
Oh yeah. Try running it client side. I ran in to that same problem last night.
[QUOTE=JTG2003;22593896]This is my current code [CODE] function KeyPressed (ply, key) if( ply:KeyDown( IN_FORWARD ) and ply:KeyDown( IN_SPEED )) then Msg("Running") end end hook.Add( "KeyPress", "KeyPressedHook", KeyPressed ) [/CODE]When I run (Pressing shift and W), it prints "Running" in the console. The problem is, I need it to be able to CONTINUOUSLY put "Running" in the console while the keys are being pressed. Lua doesn't seem to support while loops though.. Thoughts?[/QUOTE] KeyPressed is only called whenever a key has been pressed and released. You should do your checks in a Think hook instead.
[QUOTE=Crazy Quebec;22594522]KeyPressed is only called whenever a key has been pressed and released. You should do your checks in a Think hook instead.[/QUOTE] I still don't really understand the concept of hooks... [editline]10:32PM[/editline] [QUOTE=Jova;22594509]Oh yeah. Try running it client side. I ran in to that same problem last night.[/QUOTE] Hmm, the function is being called but the condition isn't being satisfied, it seems.
Whenever a certain event happen the game will "call" a hook, and any function linked to that hook will be ran with the passed arguments. Some events also request input from the hook to know if they should proceed with something or not.
[QUOTE=Crazy Quebec;22594582]Whenever a certain event happen the game will "call" a hook, and any function linked to that hook will be ran with the passed arguments. Some events also request input from the hook to know if they should proceed with something or not.[/QUOTE] So what's a Think hook?
It is run every "Think" event, which is very often. Everything is paced by Think events. Which is to say that a think hook always runs, so you'll instantly know if a player has a key down if you check it in this hook.
Sounds like it would bog down the server..
[QUOTE=JTG2003;22594706]Sounds like it would bog down the server..[/QUOTE] what?
[QUOTE=Jova;22595038]what?[/QUOTE] Think events. Called every frame...
[QUOTE=JTG2003;22595145]Think events. Called every frame...[/QUOTE] That's why you don't do expensive calculations in them, but there are already thousands of think events each frame in a regular server, so don't worry about simply using it.
[QUOTE=Crazy Quebec;22595346]That's why you don't do expensive calculations in them, but there are already thousands of think events each frame in a regular server, so don't worry about simply using it.[/QUOTE] [CODE] function doThink() Msg("taco") if( ply:KeyDown( IN_FORWARD ) and ply:KeyDown( IN_SPEED )) then Msg("Running") end end hook.Add("Think", "doThink", doThink) [/CODE] Hm. "Taco" doesn't even print
[QUOTE=JTG2003;22595946][CODE] function doThink() Msg("taco") if( ply:KeyDown( IN_FORWARD ) and ply:KeyDown( IN_SPEED )) then Msg("Running") end end hook.Add("Think", "doThink", doThink) [/CODE] Hm. "Taco" doesn't even print[/QUOTE] Where are you saving this code?
[QUOTE=grea$emonkey;22596065]Where are you saving this code?[/QUOTE] it's in my shared.lua, under "if CLIENT then" Edit: Nevermind, Im an idiot.. there's tacos everywhere now.
Ninja'd.
Well "taco" should be printing then. As for the rest did you define ply? If not place local ply = LocalPlayer() somewhere above it. An error in the hook would stop it from executing more then once. [editline]02:16AM[/editline] [QUOTE=LEETNOOB;22597706]I see the problem. You forgot to add [lua] for k, ply in pairs (player.GetAll()) do [/lua][/QUOTE] That would be right, but only if he was doing it serverside.
Sorry, you need to Log In to post a reply to this thread.