Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What is the code for a and d keys?

I would like to know the code or where to get it so people can only use a and d keys for my 2D Game.

1 answer

✓ Accepted answer

Why not just use UserInputService?

They are very simple.

Do this in a local script.

-- If working with the player
local player = game.Players.LocalPlayer
-- If you want to receive mouse input
local mouse = player:GetMouse()
-- If you are working with the player's character
local char = player.Character
-- Crucial line of code when working with the character
repeat wait() until player.Character

-- Store User Input Service in a variable
local UIS = game:GetService("UserInputService")

-- Handle user input
UIS.InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.A then -- if user presses "A"
		-- Do things when "A" is pressed
	elseif input.KeyCode == Enum.KeyCode.D -- if user presses "D"
		-- Do things while "D" is pressed
	end
end)

Keep in mind that if you would like to detect when user input has ended you will need to use the InputEnded event. If you are using loops then you will need bool values to detect when input has ended and stop the loop.

Note: I wrote this code directly in the browser. Don't be surprised if there are errors.

2 comments

0

you missed whether or not the user presses a or d when chatting, the current code does not check, because user input service has two params

0

I was aware.

This discussion is preserved as it was. New answers, comments, and votes are closed.