Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What is the code for a and d keys?

Asked by
Sei4or -3
8 years ago

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

Log in to vote
5
Answered by
AZDev 590 Moderation Voter
8 years ago

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.

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 scottmike0 40 — 8y
0
I was aware. AZDev 590 — 8y
Ad

Answer this question