I'm making a 2D game but I don't know how to disable forward and backward movement

I want to make a 2D game. I have made a lot of progress, however I’m not sure how to disable the forward and backward movement. I have a script that disables forward and backward movement but half of the time it doesn’t work. The script also switches around the left and right movement keys (a and d) because of the camera angle it needs to be that way. That aspect works every time.
I have found some tips on how to disable player movement but they only cover the first step so I’m left with no idea after that. I copied the player module and put it into StarterPlayerScripts and deleted the camera module script but I have gotten no help on what to do after that.
I’ve been trying to get help on this for a while now and this is my 3rd topic about it because I’ve been trying to find different ways to disable forward and backward player movement. I tried scripts and keybinds to the same key to cancel out forward and backward movement but it just won’t work. It’s given my a headache trying to figure out how the heck somebody is supposed to do this. I can’t find a working tutorial and I can’t find any answers that explain enough for me to understand here, so please, please, please if you reply with a solution could you make it easy to understand? (Like “You delete the code on line [number] in [whichever script has it in it].”)
This is the script I’m using:

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)

I’m desperate for some kind of answer that works.

2 Likes

You will need to access the movement script that Roblox Studio automatically puts in your game and then remove some lines from the Roblox default movement script to achieve this.

I have made a video for you to show you how this can all be done: 2D Movement tutorial

5 Likes

Thanks for showing a tutorial on how to do so. The only difference is that I got rid of forward and backward movement instead of left and right. Now with this I can develop my game further without having errors pop up. I can’t thank you enough.

2 Likes

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