Easy, first you can get a local script because of the Local Player(MUCH MUCH MUCH MUCH Easier that way, like 500x easier) then we have to get the mouse using :GetMouse()
to get the players mouse. The player's mouse has an event called the KeyDown event. This event fires when a player presses a key. The parameters can represent the key you have pressed. We can find out what key we pressed by using an if statement. You can find out a key the player presses by using either string.byte or, because the key is a string, we can see what key it is that way.
Get player from a local script.
1 | local player = game.Players.LocalPlayer |
Get mouse from player
1 | local player = game.Players.LocalPlayer |
2 | local mouse = player:GetMouse() |
Use the keydown event:
1 | local player = game.Players.LocalPlayer |
2 | local mouse = player:GetMouse() |
4 | mouse.KeyDown:connect( function () |
5 | print ( "Pressed a key." ) |
Get the key the player has pressed:
1 | local player = game.Players.LocalPlayer |
2 | local mouse = player:GetMouse() |
4 | mouse.KeyDown:connect( function (key) |
5 | print ( "Pressed: " ..key) |
Find out what key the player has pressed:
There are multiple ways of doing this.
1.) The string.byte way:
1 | local player = game.Players.LocalPlayer |
2 | local mouse = player:GetMouse() |
4 | mouse.KeyDown:connect( function (key) |
5 | if key:byte() = = 32 then |
2.) Regular way
1 | local player = game.Players.LocalPlayer |
2 | local mouse = player:GetMouse() |
4 | mouse.KeyDown:connect( function (key) |
3.) And the Regular way plus string.lower() or string.upper()
1 | local player = game.Players.LocalPlayer |
2 | local mouse = player:GetMouse() |
4 | mouse.KeyDown:connect( function (key) |
5 | if key:lower() = = "e" or key:upper() = = "E" then |
6 | print ( "Pressed e or E" ) |
KeyDown is not recommended to be used, use Keyboard input instead.
To use this it's easy,(Can be in normal script in workspace or something.)
1 | game:GetService( "UserInputService" ).InputBegan:connect( function (inputObject, gameProcessedEvent) |
2 | if inputObject.KeyCode = = Enum.KeyCode.R then |
Just use the userimputservice which also has other cool features like an event fires if someone on a touchscreen device pinches the screen it fires.
If you are using keydown still and is wanting to know how do I know that 32 is space? I got it off of this roblox wiki page. Look under the "DEC" column. http://wiki.roblox.com/index.php?title=File:Ascii_Table.png
Hope this helps!
Locked by WideSteal321, aquathorn321, and royaltoe
This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.
Why was this question closed?