Temporarily disable forward/backward Movement

Hello, I’m trying to temporarily disable forward/backward movement, but I can’t find a way to do. I searched up on devforum, but the solutions were all permanent. Is there a way I can make it so it can be toggled?

Thank you.

1 Like

You can achieve this by using ContextActionService which “allows an experience to bind user input to contextual actions, or actions that are only enabled under some condition or period of time.” Because this system allows you to set multiple actions to one context, and control whether following actions should execute using their priority system, you can set an action that is at a higher priority then the character core movement context that overrides and stops the movement action from executing by returning Enum.ContextActionResult.Sink from your action.

Here’s a function that allows you to toggle character movement:

--[[ Local Script ]] --
local ContextActionSerivce = game:GetService("ContextActionService")

local function ContextSink()
	return Enum.ContextActionResult.Sink
end

function setMovementRestraint(enabled)
	if enabled then
		ContextActionSerivce:BindActionAtPriority(
			"RestrictedMovement", 
			ContextSink, 
			false,  
			2001,
			Enum.PlayerActions.CharacterForward,
			Enum.PlayerActions.CharacterBackward
		)
	else
		ContextActionSerivce:UnbindAction("RestrictedMovement")
	end
end

print("Stopping forward/backward movement")
setMovementRestraint(true)

task.wait(5)

print("Enabling forward/backward movement")
setMovementRestraint(false)
1 Like

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