Disabling specific movement keys

For my game, I need to completely disable the W and S keys, so players can only move to the left or right, and jump. All I’ve been able to do so far is disable movement altogether, but I can’t figure out how to disable specific keys.

7 Likes

This is easy to do because movement is done through core scripts.

Simply run:

ContextActionService:UnbindAction("forwardMovement")
ContextActionService:UnbindAction("backwardMovement")
19 Likes

Do I need to write a new script containing this, or edit an existing core script?

2 Likes

Write a new script.

2 Likes

Could you please tell me exactly what needs to be in the script, and where the script is supposed to be? I know I’m doing something wrong, but I don’t know what.

2 Likes

As long as that’s in a LocalScript, you can use game:GetService("ContextActionService") to get the service I mentioned. Then, when you want to disable movement, run the code I showed.

3 Likes

Then how would i bind it back?

2 Likes

If you have intentions to bind it back, then don’t remove the context binding. What you can do instead is create an action binding with a higher priority for the W and S keys which returns Enum.ContextActionResult.Sink. This will prevent other bindings from running if they have a lower priority.

5 Likes

Oh, i’ve heard about this, but how would i disable the left/right movement for game pad and mobile using this?

1 Like

Gamepads and mobile require a bit of a different process, since they rely on the delta distance of moving a control stick. This may require forking of the control scripts to accomplish.

3 Likes