Nullify player input / cancel its effect

Ok, I’m making a 2d sidescroller. The movement controls work great, except when the player holds w or s (which are forward/back in the first person perspective, but from the 2d sidescroller perspective it’s up/down) they move to the right/left. Is there a function where I can cancel the player’s key presses? If not, what is another way I can do it?

As far as I know, there isn’t a function specifically for it, but I did find a way to do this for an earlier gamemode that I worked on.

Unfortunately, I’m not at my home computer, so I can’t help you.

It involves the OnBindPressed hook, though (clientside). If you don’t get it figured out by the time I get home (in about two hours), then I’ll post what I used.



I overcomplicated things.

Returning in PlayerBindPress does the job.

thanks, i’ll try playerbindpress




Except then you’d probably need to animate the character yourself.

That works for stopping the player’s forward/backward movement, but I still need to execute some code when the player presses +back! How would I do that then?

Try **[Gamemode.KeyPress](**.


And **[Player.KeyDown](**.

I’m not going to lock the player when they hit down. Wouldn’t that cancel all sideways movement?

I used FlapJack’s function, not your’s. Tried executing code before returning in that function, but nothing worked.



Example: **[CUserCmd.SetButtons](**

These should fit your need perfectly.

Here. This worked for me.

Note that the circumstances I used this in was that the player was locked (via Player:Lock()). I was using it to disable shooting whilst remote-controlling a turret.

[lua]nBindParse = 0
PilotBindCommand = {
– Bind stuff (for the turrets and such)
function CCGM_BindPress(user,bind,press)
if user.Piloting then
for k,t in pairs(PilotBindCommand) do
if string.find(bind,t[1]) then
local down = “0”
if press then down = “1” end – If the binds are restricted, such as when piloting, the “press” returns true when the button is pressed down, and false when the button is released.
nBindParse = CurTime() + 0.1

	return true

hook.Add(“PlayerBindPress”,“CCGM Player Bind Press”,CCGM_BindPress)[/lua]

Adjust to fit your needs as required. It appears that returning anything means that the bind won’t execute (judging from this code; it’s been a while since I did it).


How does SetButtons work? I read the wiki page and don’t understand what the “|” and “&” symbols mean. I know that || and && are or/and. But I don’t know what they mean individually.

They are bitwise operators, they act on the individual bits of the operands.

& is a bitwise AND. It performs a logical and on each bit, for example:

01010111010 &

| is a bitwise OR. It performs a logical or on each bit, for example:

01010111010 |

FFFFFFF, :ninja:. That’s what I get for looking things up :(.

| and & are bitwise operators ( as are << and >> ).

| Is BOR, bitwise or, which performs a logical or on both bits, effectively adding them together.
& is BAND, bitwise and, which pefrorms a logical and on both bits, a “mask” if you will.

If you are making a convar or setting npc capabilities, you would use |:


Which is pretty much the same as:


You would use & when you want to check if a bit is inside of another bit:

if pl:GetFlags( ) & FLAG_POLICE == FLAG_POLICE then
  pl:Give( "weapon_arreststick" )

Of course, pl:GetFlags( ) and FLAG_POLICE are not defined but are just methods used in this example.

In plain code, this would be something like:

num % (bit + bit) >= bit

Hope that helps :).

Technically, that is wrong. Bitwise OR performs a logical OR on each bit in a series of bits, it only only has the effect of adding the series of bits together when both series don’t have any of the same bits set. This is why enumerations are only ever powers of two, because each power of two when represented in binary is just one bit set.

1   = 00000001
2   = 00000010
4   = 00000100
8   = 00001000
16  = 00010000
32  = 00100000
64  = 01000000
128 = 10000000

True, will fix that.

What if I just did:

if LocalPlayer():KeyDown( IN_FORWARD ) then
{====Code to execute after pressing forward goes here====}
cmd:SetButtons(cmd:GetButtons() - IN_FORWARD)
–Will this cancel out the player’s forward movement?
–And if it does will the code above it still execute?
–And if it doesn’t, how would I make it so it does execute while still cancelling movement?

Any help would be great. The thing is I want the player to be able to dig down while pressing IN_BACK and jump when they press IN_FORWARD, but I don’t want the player to move left/right. I tried several methods and none worked. Any code you can provide would be nice :wink: