How can I rebind the space key to jump after unbinding it?

Hello, I am making a system that is meant to lock a player in their seat when certain conditions are true. I attempted to do this by unbinding and rebinding the space key. While the space key unbinds with no issue, I am unable to bind it back. Here is my current script, preferably, I’d want to bind the key back without using a function to do so if possible. Thank you.

local ContextActionService = game:GetService('ContextActionService')
ContextActionService:UnbindAction("jumpAction")

ContextActionService:BindAction("jumpAction",Enum.KeyCode.Space)

(Setting JumpPower to 0 will not work in my circumstance)

The only way to rebind the jump action is to bind the original function. However, you can simply block the space input by binding it at a high priority and sinking it every time it comes up.
Example code:

game:GetService("ContextActionService"):BindActionAtPriority("BlockSpace",function()
	return Enum.ContextActionResult.Sink
end,false,9999999,Enum.KeyCode.Space)

And to unblock:

game:GetService("ContextActionService"):UnbindAction("BlockSpace")
1 Like

Sorry for the late reply, internet was down. Your system works, thank you for the help. Mind explaining with a bit more detail how it functions, as I have never used this before and would like to learn. Thank you.

When multiple actions are bound to a key, the callbacks are called in order. If one of the callbacks returns Enum. ContextActionResult.Sink, the input is considered sunk and the remaining callbacks will not run. I believe actions bound at a priority come before normally bound actions, but I made sure by binding it at priority 99999999 (higher priority = called first).

Hope this could help!

1 Like

What about mobile controls? I doubt blocking the spacebar does anything regarding touchscreens.

I apologize for my earlier solution, as I completely forgot about SetStateEnabled. You can disable the Humanoid’s jumping state on the client with humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false), and renable it humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, true). This should definitely be used over disabling space.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.