How do I bind a movement key to another movement key?

I am making a 2D game. I want to make it so that both the forwards and backwards movements are binded to the w key.

I have a script that fails half of the time when I play my game allowing the player to move forwards and backwards. What does work is the keybinds. I have left binded to d and right binded to a (the opposite keys due to camera angle). Because of that I want to bind forwards and backwards to the same key in order to cancel each other out.

I looked for some answers but I couldn’t find much. All I found was this post which I could not understand.

I forget where I got the script but I think I got it from here. I modified it to try and bind forward and backwards together.
Original Script:

local player = game.Players.LocalPlayer
local RunService = game:GetService("RunService")
local ContextActionService = game:GetService("ContextActionService")

local jumping = false
local leftValue, rightValue = 0, 0

local function onLeft(actionName, inputState)
	if inputState == Enum.UserInputState.Begin then	
		leftValue = 1
	elseif inputState == Enum.UserInputState.End then
		leftValue = 0
	end
end

local function onRight(actionName, inputState)
	if inputState == Enum.UserInputState.Begin then
		rightValue = 1
	elseif inputState == Enum.UserInputState.End then
		rightValue = 0
	end
end

local function onJump(actionName, inputState)
	if inputState == Enum.UserInputState.Begin then
		jumping = true
	elseif inputState == Enum.UserInputState.End then
		jumping = false
	end
end

local function onUpdate()
	if player.Character and player.Character:FindFirstChild("Humanoid") then
		if jumping then
			player.Character.Humanoid.Jump = true
		end
		local moveDirection = rightValue - leftValue
		player.Character.Humanoid:Move(Vector3.new(moveDirection,0,0), false)
	end
end

RunService:BindToRenderStep("Control", Enum.RenderPriority.Input.Value, onUpdate)

ContextActionService:BindAction("Left", onLeft, true, "d", Enum.KeyCode.Left, Enum.KeyCode.DPadLeft)
ContextActionService:BindAction("Right", onRight, true, "a", Enum.KeyCode.Right, Enum.KeyCode.DPadRight)
ContextActionService:BindAction("Jump", onJump, true, " ", Enum.KeyCode.Space, Enum.KeyCode.Up, Enum.KeyCode.DPadUp, Enum.KeyCode.ButtonA)

Edited Script:

local player = game.Players.LocalPlayer
local RunService = game:GetService("RunService")
local ContextActionService = game:GetService("ContextActionService")

local jumping = false
local leftValue, rightValue, forwardValue, backwardValue = 0, 0, 0, 0

local function onLeft(actionName, inputState)
	if inputState == Enum.UserInputState.Begin then	
		leftValue = 1
	elseif inputState == Enum.UserInputState.End then
		leftValue = 0
	end
end

local function onRight(actionName, inputState)
	if inputState == Enum.UserInputState.Begin then
		rightValue = 1
	elseif inputState == Enum.UserInputState.End then
		rightValue = 0
	end
end

local function onFoward(actionName, inputState)
	if inputState == Enum.UserInputState.Begin then	
		forwardValue = 1
	elseif inputState == Enum.UserInputState.End then
		forwardValue = 0
	end
end

local function onBackwards(actionName, inputState)
	if inputState == Enum.UserInputState.Begin then	
		backwardValue = 1
	elseif inputState == Enum.UserInputState.End then
		backwardValue = 0
	end
end

local function onJump(actionName, inputState)
	if inputState == Enum.UserInputState.Begin then
		jumping = true
	elseif inputState == Enum.UserInputState.End then
		jumping = false
	end
end

local function onUpdate()
	if player.Character and player.Character:FindFirstChild("Humanoid") then
		if jumping then
			player.Character.Humanoid.Jump = true
		end
		local moveDirection = rightValue - leftValue - forwardValue - backwardValue
		player.Character.Humanoid:Move(Vector3.new(moveDirection,0,0), false)
	end
end

RunService:BindToRenderStep("Control", Enum.RenderPriority.Input.Value, onUpdate)

ContextActionService:BindAction("Left", onLeft, true, "d", Enum.KeyCode.Left, Enum.KeyCode.DPadLeft)
ContextActionService:BindAction("Right", onRight, true, "a", Enum.KeyCode.Right, Enum.KeyCode.DPadRight)
ContextActionService:BindAction("Forward", onFoward, true, "w", Enum.KeyCode.Up, Enum.KeyCode.DPadUp)
ContextActionService:BindAction("Backwards", onBackwards, true, "w", Enum.KeyCode.Down, Enum.KeyCode.DPadUp)
ContextActionService:BindAction("Jump", onJump, true, " ", Enum.KeyCode.Space, Enum.KeyCode.Up, Enum.KeyCode.DPadUp, Enum.KeyCode.ButtonA)

When I tested this out pressing the w key moved the player right. I have 2 options technically: 1 being binding forwards and backwards to the same key making neither work and 2 being I use a script that works every time (maybe the script fails every time because it needs to wait for the player to load in) and setting the players speed to 10 in it as well.
I hope I did well with this post and explained everything well enough. If I missed something that may be helpful to know then please comment to let me know and I will edit it into my post.

1 Like