Unbinding the backwards key

I’d like to disable the “s” key (and also the down arrow) to prevent the player to go backwards because it just looks silly as there is no specific animation when going backwards in first person making it very weird. To my surprise there seems no way that I know to do it so I’d like help. I tried looking at the wiki but it seems very unclear.

3 Likes

I would personally advise against doing this since being able to walk backwards is a well-established behaviour in games; I personally do it all the time, and you see lots of people doing it on Roblox and in other games.

You could possibly look into making a custom animation for walking backwards if you’d like, which would also solve your issue :slightly_smiling_face:

4 Likes

Despite what @Elttob said, if you’re insistent on disabling backwards movement, you can unbind the S key from its function. Movement keys are Lua-implemented, so you can unbind them if you’d like.

local ContextActionService = game:GetService("ContextActionService")
ContextActionService:UnbindAction("moveBackwardAction")

Alternatively, if you’d like to restore the functionality later, you can instead push a higher-priority action to the backwards motion. You can either unbind the action once it’s no longer needed or set up a condition that sinks or passes the higher priority bind after.

local ContextActionService = game:GetService("ContextActionService")

-- Creating:
ContextActionService:BindActionAtPriority("disableBackwardMovement", function()
    return Enum.ContextActionResult.Sink
end, false, Enum.ContextActionPriority.Default.Value + 50, Enum.PlayerActions.CharacterBackward)

-- Removing:
ContextActionService:UnbindAction("disableBackwardMovement")

-- With condition:
local AllowBackwardMovement = false

ContextActionService:BindActionAtPriority("disableBackwardMovement", function()
    return AllowBackwardMovement and Enum.ContextActionResult.Pass or Enum.ContextActionResult.Sink
end, false, Enum.ContextActionPriority.Default.Value + 50, Enum.PlayerActions.CharacterBackward)
23 Likes

What is your resource to finding out what the currently bound actions were along with their names?

The ActionBindings tab of your F9 Developer Console. This tab shows you a list of bindings from ContextActionService in a list format, including:

  • Name (have no idea why this is here)
  • Priority level (default ContextActionPriority is 2000)
  • Security level (currently in reverse order)
    • Developer: Bindings set by CoreScripts
    • Core: Bindings set by non-CoreScripts
  • Action name (typically the first argument of any ContextActionService bind method)
  • Input types that can be used to activate this action

Considering that PlayerScripts are fully Lua-implemented, the binds can be overridden or changed as well. Once you have the names, you can just roll with that. Unbind, bind over them, do whatever you want.

In case you’re curious about the way Roblox binds actions, you can check out the Keyboard module of the ControlScript which manages keyboard-related actions. That’s available in the RobloxPlayerScript repository. Or, you can use the common method of forking from a Play Solo session to get the module.

Relevant binding chunks are highlighted for your convenience.

BindAction is equivalent to using BindActionAtPriority with the value of Enum.ContextActionPriority.Default.

3 Likes

So I did

local ContextActionService = game:GetService(“ContextActionService”)
ContextActionService:UnbindAction(“moveBackwardAction”)

in a LocalScript put in the workspace but [S] still moves me backwards.

LocalScripts don’t function in the workspace.

Ok so what should I do to get it working Nevermind I put it in StarterGui and It worked.

I. Am. Allowed. To. Bump
This isnt working anymore. what do i do? its in a localscript in starterplayer scripts and very frustrating

Consider debugging your code or detailing your circumstances more if you’re looking for help. This still works - PlayerModule still binds movement actions via ContextActionService (you can check this via F9 → ActionBindings). Try waiting some time before unbinding or moving it to a different location.

At the start of the script I wait till characterized fires. After that I unbind it. Also tested in a separate localscript in startergui.

It doesn’t show the s key, but if I press S it still stops me. I am using a custom character with a custom movement system, does it matters?

It’s not binded under the S key. Please check your console and my reply again carefully. Furthermore, you really need to debug your code first. If debugging doesn’t help out, then you can ask for help but you need to supply extensive details about your circumstance. You aren’t providing enough information for me to work from. Without such details, I cannot help you.

1 Like

Sorry. I will investigate more next time.