Disabling Forward and Backward key binds for a platformer Game

Hello Developers,
I am making a Platformer game.(kinda like epic minigames, but as a platformer) I want to make it so the player can only move left and right. (and jump) I already coded it so that pressing keys “W” and “S” do not move the player. I also replaced the mobile Modal with onscreen GUI’s that when pressed, make the player go left, right, and jump. Is there any known way to universally make it so the player can’t move forward and backward? And when the round ends, could I rebind the forward and backward key binds?

Although it is currently working for mobile and Keyboard/mouse, I want to add Xbox support too. This could be as simple as making it so the player can only move across the x or z axis. (But I don’t know where to start when coding this idea)

Thanks for reading!

1 Like

I used to bind an empty function to the W and S key via contextactionservice and unbind it to make it go back to normal but i just use a table of inputs instead and loop. i am slow today i needa phrase this better.

But i dont think there is any universal way to move forward n backwards with every device

  1. Go into play testing.
  2. Go to Explorer → Players → (Your player) → PlayerScripts
  3. Copy the scripts, stop the playtest, and paste them into game.StarterPlayer.PlayerScripts
  4. Go to the module script PlayerModule → ControlModule → Keyboard
  5. From line 89, replace with this
local handleMoveForward = function(actionName, inputState, inputObject)
		if allowForwardBackward then
			self.forwardValue = (inputState == Enum.UserInputState.Begin) and -1 or 0
			self:UpdateMovement(inputState)
		end
		return Enum.ContextActionResult.Pass
	end

	local handleMoveBackward = function(actionName, inputState, inputObject)
		if allowForwardBackward then
			self.backwardValue = (inputState == Enum.UserInputState.Begin) and 1 or 0
			self:UpdateMovement(inputState)
		end
		return Enum.ContextActionResult.Pass
	end

For xbox,
6. Go to PlayerModule > ControlModule > Gamepad.
7. On line 112, change to

local yPosition = allowForwardBackward and -inputObject.Position.Y or 0
			self.moveVector  =  Vector3.new(inputObject.Position.X, 0, 0)

Now all you need to do is set allowForwardBackward and have these modules access it’s current state.
Hope that helps!

1 Like

Thanks a lot! Can I also copy the Xbox script into the mobile script? Nevermind, I found an answer

For anyone with the same question, you just override the direction of the thumbstick.

replace

currentMoveVector = Vector3.new(currentMoveVector.x, 0, currentMoveVector.y)

with

if allowForwardBackward then
	currentMoveVector = Vector3.new(currentMoveVector.x, 0, currentMoveVector.y)
else
	currentMoveVector = Vector3.new(currentMoveVector.X,0,0)
end

but you do have to fix the camera for this to work

You don’t need to fork the official scripts to achieve this, you can simply unbind the move forward and move backward actions by using the following code sample.

local Game = game
local ContextActionService = Game:GetService("ContextActionService")

local function OnAction(ActionName, InputState, InputObject)
	print("Hello world!")
end

ContextActionService:UnbindAction("moveForwardAction")
ContextActionService:UnbindAction("moveBackwardAction")

ContextActionService:BindAction("Action", OnAction, true, Enum.KeyCode.W, Enum.KeyCode.S)

Bare in mind, you must bind both input keys to another action otherwise the default binds will be reverted to.

However. this solution will not work on Xbox or mobile devices

You can disable the movement button on mobile & I’m fairly sure Xbox uses the same (or similar) binds to desktop.