How do you make movement only based off of was and space bar?

I was wondering how would you disable arrow key movement and make only wasd and space bar movement. Is this even possible?

I don’t know a whole lot about modifying the default control module for the player, so I’ll tell you my way of doing this.

You can start by disabling the default input of the PlayerModule, like so:

local controls = require(game:GetService("Players").LocalPlayer.PlayerScripts.PlayerModule):GetControls()
controls:Enable(false) 

Then, you can utilize UserInputService to set up your own binds with the WASD keys.

Client Code

local uis = game:GetService("UserInputService")
local movement = {
["A"] = {down = false, dir = Vector3.new(-1, 0, 0)};
["D"] = {down = false, dir = Vector3.new(1, 0, 0)};
["W"] = {down = false, dir = Vector3.new(0, 0, 1)};
["S"] = {down = false, dir = Vector3.new(0, 0, -1)};
}

local function Update_Player_Movement() --//Used to set player moving direction
local move_direction = Vector3.new(0, 0, 0)
for i, input in pairs(movement) do
if input.down then
move_direction = move_direction + input.dir --//Stack directions on top of each other. Should work.
end
end
player.Character.Humanoid.MoveDirection = move_direction
end

uis.InputBegan:Connect(function(input, g)
if not g then
if movement[input.KeyCode.Name] then
movement[input.KeyCode.Name].down = true
Update_Player_Movement()
end
end
end)

uis.InputEnded:Connect(function(input)
if movement[input.KeyCode.Name] then
movement[input.KeyCode.Name].down = false
Update_Player_Movement()
end
end)

I rushed through this, so I can’t guarantee that it’ll work, but it’s a start.

Hope this helps. :slight_smile:

EDIT:

Just realized I didn’t set up inputting the correct move direction for the humanoid…I’m kind of dumb.

Use context action service, bind action that returns Enum.ContextActionResult.Sink to all 4 arrow key inputs

2 Likes

The simplest solution is using ContextActionService. I unpacked the array here because the line got too long and I am a neat freak.

local ContextActionService = game:GetService("ContextActionService")

local Arguments = {false, Enum.ContextActionPriority.High.Value, Enum.KeyCode.Up, Enum.KeyCode.Down, Enum.KeyCode.Left, Enum.KeyCode.Right}

ContextActionService:BindActionAtPriority("DisableArrowKeys", function()
	return Enum.ContextActionResult.Sink 
end, unpack(Arguments))

If you put that in a LocalScript inside of StarterPlayerScripts(or any place where LocalScripts run), it should work. I tested it in studio to make sure.

7 Likes