Reversing the controls

Hi! I am a new roblox programmer and developer and i am trying to make my first real game. I decided to make it an obby but with a twist, your controls are reversed. So when you press w to walk forward, you go back and also on mobile. But because i am a new developer i struggled with making a script that changes the controls.

Here is my try with the limited knowledge i have from youtube videos and such.


local function reverseControls(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")


		local userInputService = game:GetService("UserInputService")
		
		local function onInput(input)
			if input.UserInputType == Enum.UserInputType.Keyboard then
				if input.KeyCode == Enum.KeyCode.W then
					humanoid:Move(Vector3.new(0, 0, -1))
				elseif input.KeyCode == Enum.KeyCode.S then
					humanoid:Move(Vector3.new(0, 0, 1))
				elseif input.KeyCode == Enum.KeyCode.A then
					humanoid:Move(Vector3.new(1, 0, 0))
				elseif input.KeyCode == Enum.KeyCode.D then
					humanoid:Move(Vector3.new(-1, 0, 0))
				end
			elseif input.UserInputType == Enum.UserInputType.Touch then
				-- Reverse mobile joystick controls
				if input.Position.X < (game:GetService("GuiService"):GetScreenResolution().X / 2) then
					humanoid:Move(Vector3.new(-input.Delta.Position.X, 0, 0))
				else
					humanoid:Move(Vector3.new(0, 0, -input.Delta.Position.X))
				end
			end
		end

		userInputService.InputBegan:Connect(onInput)
	end)
end


for _, player in pairs(game:GetService("Players"):GetPlayers()) do
	reverseControls(player)
end


game:GetService("Players").PlayerAdded:Connect(reverseControls)

I put this in a script inside ServerScriptService

I don’t know if this is close or if im completely wrong.

If you help me i will give you credits in the description as a thanks.

I’m pretty sure that UserInputService can only be used on client

I tried now to put the code in a localscript in both startercharacterscripts and starterplayerscripts but nothing seems to happen

here i think this will help

you’re using playerAdded, which is a server sided event im pretty sure. Just do players.localplayer and instantly call the function.

I tried doing so and i am getting the error Players.Monkebamium.PlayerScripts.LocalScript:39: attempt to index function with ‘localplayer’

you might’ve understood me wrong

reverseControls(game:GetService("Players").LocalPlayer)

Now i am not getting any errors but nothing happens. I am sorry for not understanding much

I believe I have made this before, you simply clone the controls module in a playtest and edit.

  1. Start a playtest
  2. Go to players → player → playerscripts → copy the “PlayerModule”
  3. Paste it in starterplayerscripts, go to control module → keyboard and on like 121-130 mix up the actions.
1 Like

In fact, I just made it. Replace 121-130 of the Keyboard module with

-- movement direction is done in Lua
	ContextActionService:BindActionAtPriority("moveForwardAction", handleMoveBackward, false,
		self.CONTROL_ACTION_PRIORITY, Enum.PlayerActions.CharacterForward)
	ContextActionService:BindActionAtPriority("moveBackwardAction", handleMoveForward, false,
		self.CONTROL_ACTION_PRIORITY, Enum.PlayerActions.CharacterBackward)
	ContextActionService:BindActionAtPriority("moveLeftAction", handleMoveRight, false,
		self.CONTROL_ACTION_PRIORITY, Enum.PlayerActions.CharacterLeft)
	ContextActionService:BindActionAtPriority("moveRightAction", handleMoveLeft, false,
		self.CONTROL_ACTION_PRIORITY, Enum.PlayerActions.CharacterRight)
	ContextActionService:BindActionAtPriority("jumpAction", handleJumpAction, false,
		self.CONTROL_ACTION_PRIORITY, Enum.PlayerActions.CharacterJump)
1 Like

It worked! Thank you so much, i will include credits for you in the description as a thanks even though it probably wont go viral or something.

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