Problem with disabling the player moving forward or backwards by pressing Keys

I want to disable the movement of going forward and backward by pressing W and S
and I tried to use

    local MoveService=game:GetService("ContextActionService")
    MoveService:UnbindAction("forwardMovement")
    MoveService:UnbindAction("backwardMovement")

in a LocalScript i tried to put it in ReplicatedFirst and ServerScriptService but it doesnt work.
is that because I put it in the wrong location?

6 Likes

i don’t think the current core scripts use those names anymore in the first place but even if it did… The reason putting your local script in the Replicated First doesn’t work is because, well, anything in the replicated storage replicates first before the character scripts are loaded, and before any movement keys are binded, meaning that unbinding before a bind actually exists won’t do anything. You could yield and wait for everything to load and what not, but i’d argue the replicated first storage isn’t ideal for that. Placing your local script in the ServerScriptService doesn’t work because as the name implies it’s for server scripts, not local scripts. You should be placing your local script somewhere like the starter character/player scripts or the StarterGui.

However, whether or not the snippet you provided actually works or not i think a much more elegant solution to your problem would to sink the enums associated with forward and backward character movement instead, which yields the ability to enable and disable player movement more dynamically and i would say reduce the risk that a core script update could break your script.

You can sink the foward and backward keys by doing:

local ContextActionService =game:GetService("ContextActionService")
local FowardMovement = Enum.PlayerActions.CharacterForward
local BackwardMovement = Enum.PlayerActions.CharacterBackward

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

----Sink forward movement 
 ContextActionService:BindAction("SinkFowardMovement", Sink,false,FowardMovement)

----Sink backward movement 
 ContextActionService:BindAction("SinkBackwardMovement",Sink,false,BackwardMovement)

And to unsink (allow the player to move again you can unbind the action using the name you gave it ("SinkBackwardMovement" or "SinkFowardMovement" in this case)

---Alow the player to move freely again
ContextActionService:UnbindAction("SinkFowardMovement")
ContextActionService:UnbindAction("SinkBackwardMovement")

Edit:
Just some extra information and thoughts, unbinding or sinking any of the Playeractions (including using the name associated with the bindings) actually don’t affect mobile or console player movement, because they don’t use any of those enums, but most importantly they don’t use separate enumerations for movement direction (unlike keyboard movement), console thumbstick movement only uses one enum to bind to Enum.KeyCode.Thumbstick1 and infact i don’t think the mobile thumbstick uses action binding at all!

Also if you were wondering the new names for keyboard player movement is:

	"moveForwardAction"
	"moveBackwardAction"
	"moveLeftAction"
	"moveRightAction"
	"jumpAction"

But like i said, sinking is Likely better in the case that these names are changed and because of the other reasons i mentioned above.

18 Likes

Woah, i put it in StarterCharacterScripts and it worked! Thank you for helping me ! :tongue:

1 Like

P.S: The Mobile/Tablet jump button will still be shown despite Unbinding or Sinking it. The only way to change it’s visability is via assigning the JumpPower to 0.

1 Like

Good thing is that i set the JumpPower to 0 from earlier.
Thanks for telling me tho!

2 Likes

Sorry for the bump in the ancient topic, but all related recent posts point here, I required an answer, and the provided solution didn’t work on mobile/consoles.

So here’s an implementation I came up with that should work on all devices in case anyone still needs it:

-- Setup Alignment --

local function SetupAlignment()
	AlignAtt = Instance.new("Attachment"); AlignAtt.Parent = RootPart
	AlignRot = Instance.new("AlignOrientation"); AlignRot.Parent = AlignAtt
	AlignRot.Mode = Enum.OrientationAlignmentMode.OneAttachment; AlignRot.Attachment0 = AlignAtt
	AlignRot.Enabled = false
	AlignRot.MaxTorque = 1000000 --RigidityEnabled rotates inconsistently
	AlignRot.Responsiveness = 50
end
SetupAlignment()

-- Run --

local function Run(dt)
	if BackwardsDisabled then --Disable walking backwards
		
		--Vars
		local RawMoveDir = Humanoid.MoveDirection
		local MoveDir = Vector3.new(-RawMoveDir.X, 0, RawMoveDir.Z)
		local RawCamDir = Camera.CFrame:VectorToObjectSpace(RawMoveDir)
		local CamDir = Vector3.new(math.round(RawCamDir.X), math.round(RawCamDir.Y), math.round(RawCamDir.Z))
		
		if (MoveDir.Magnitude > 0) then
			if (CamDir.Z ~= 1) then --If we are not going backwards
				if not AlignRot.Enabled then
					AlignRot.Enabled = true
				end
				AlignAtt.CFrame = CFrame.lookAt(Vector3.zero, MoveDir)
				Humanoid.WalkSpeed = StoredWalkSpeed
			elseif (CamDir.Z == 1) then --If we are going backwards
				Humanoid.WalkSpeed = 0
			end
		end
	else --Re-enable walking backwards
		Humanoid.WalkSpeed = StoredWalkSpeed
		AlignRot.Enabled = false
	end
	
	--Autorotate (not even needed but I'll disable it anyways)
	Humanoid.AutoRotate = not BackwardsDisabled
end
RunService.Heartbeat:Connect(Run)

Here’s footage of it working on mobile:
https://gyazo.com/d0b040de0b23f82b9dd7e4e6623f9d60

And here’s the full demo file (press E to enable/disable walking backwards):
Disable Direction Demo.rbxl (54.0 KB)

3 Likes

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