Hi. How would a script detect if a player has pressed a key on their keyboard? Is there a set function for this?
Thanks!
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.
local player = game.Players.LocalPlayer --simple
Get mouse from player
local player = game.Players.LocalPlayer local mouse = player:GetMouse() --Also pretty simple.
Use the keydown event:
local player = game.Players.LocalPlayer local mouse = player:GetMouse() mouse.KeyDown:connect(function() print("Pressed a key.") end)
Get the key the player has pressed:
local player = game.Players.LocalPlayer local mouse = player:GetMouse() mouse.KeyDown:connect(function(key) print("Pressed: "..key) end)
Find out what key the player has pressed:
There are multiple ways of doing this.
1.) The string.byte way:
local player = game.Players.LocalPlayer local mouse = player:GetMouse() mouse.KeyDown:connect(function(key) if key:byte() == 32 then --Since you cannot type [[key == "Space"]] just use byte. print("Pressed space") end end)
2.) Regular way
local player = game.Players.LocalPlayer local mouse = player:GetMouse() mouse.KeyDown:connect(function(key) if key == "e" then print("Pressed e") end end)
3.) And the Regular way plus string.lower() or string.upper()
local player = game.Players.LocalPlayer local mouse = player:GetMouse() mouse.KeyDown:connect(function(key) if key:lower() == "e" or key:upper() == "E" then print("Pressed e or E") end end)
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.)
game:GetService("UserInputService").InputBegan:connect(function(inputObject, gameProcessedEvent) if inputObject.KeyCode == Enum.KeyCode.R then --Also, could be written as [[inputObject.KeyCode == "R"]] print("R was pressed") end end)
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!
Hello, like what above said, you should use the service, UserInputService instead of keyDown.
Here's an example script:
game:service'UserInputService'.InputBegan:connect(function(inputObject, gameProcessedEvent) if (inputObject.KeyCode=='W') then print'Hi.'; end; end);
What this would do, exactly, is to check if you pressed the key 'W' on your keyboard, and if you did, it would print the text "Hi."
Actually, there's a better way to do this than GetMouse because it allows you to choose when the key can fire and when it can't. It's called Context Action Service, you can learn to use it here. For example, if you were making a tool and you want to be able to press f to fly you can make that and make it so it only works when the tool is equipped. AND, you can add a button for people on mobile devices to use since they don't have keyboards.
In localscript
local plr = game.Players.LocalPlayer -- Player where the script is running local mouse = plr:GetMouse() -- Their mouse mouse.KeyDown(function(w) -- do stuff end)
This should work
NOTE: it appears KeyDown is deprecated in favor of ContextActionService Also i'm a little rusty on my Lua
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?