So, for example, I want to create a function that fires every time a user presses one. Here's my code so far.
mouse.KeyDown:connect(function(key) if key == "f" then -- Code end end)
Except, I want to change the "f" into a one.
With KeyDown, it's not possible to get the number unless you disabled the Backpack. The reason for KeyDown not working with the numbers is because the key is being utilized by ROBLOX's Core Services and ROBLOX didn't want other scripts to interfere I guess.
So if you want to go the KeyDown route which I would advise against you would want to disable the Backpack CoreGui and compare the key's byte code to the byte code of one. Also, with your code, make sure you're getting the mouse otherwise you're calling KeyDown on a nil value.
game.StarterGui:SetCoreGuiEnabled('Backpack',false) --Backpack will no longer function. game.Players.LocalPlayer:GetMouse().KeyDown:connect(function(key) --Get the player's mouse and connect it to the KeyDown event. if key:byte() == 49 then --If 1 is pressed then the script will run. 49 is the byte code of the string '1' print'It Works!' end end)
This one is a little trickier to set up however it is a more advanced method of getting a person's input. You will want to use a LocalScript for this as you would need for the KeyDown Method.
With UserInputService
you will want to establish the InputBegan event. Since UserInputService is not automatically in DataModel, you will need to use the GetService()
function. The InputBegan function returns two variables. One being the key information that was pressed, and the other being if the key being pressed is being handled by the game. For this example, the second variable can be ignored. The first variable returns an array of information which you can find here. Since we are wanting to find out the key pressed, we will only want to utilize the KeyCode value. We can then compare the key that was pressed to an Enumerator code.
game:GetService('UserInputService').InputBegan:connect(function(key,processed) --Establishing event. if key.KeyCode == Enum.KeyCode.One then --Comparing the key that was pressed. print'It Works!' end end)
Numbers are the word version of themselves, so if the player presses '1' they'll get 'one'. Just refer to this: http://wiki.roblox.com/index.php?title=API:Enum/KeyCode
So:
mouse.KeyDown:connect(function(key) if key == "one" then -- Code end end)
Just note, KeyDown is deprecated, so you should try using ContextActionService, or UserInputService.
EDIT:
Like I said above, KeyDown is deprecated, so it may not work. But, the problem may be that you have not put the key into lowercase (the key can sometimes be 'ONE' instead of 'one').
So, add this straight under the start of the function, above the if statement:
key = key:lower()
Don't use KeyDown, that's deprecated. Instead, use UserInputService. Sure, UIS may sound complicated, but it's not- it's more useful than it is complicated. Note: UIS can only be accessed in LocalScripts.
local player = game.Players.LocalPlayer local uis = game:GetService("UserInputService") uis.InputBegan:connect(function(t) --Fires when the user starts an input, etc. a keypress. t is a table returned by the function that has other things, but we'll be focusing on t.KeyCode. if t.KeyCode == Enum.KeyCode.One then --If the keycode represented by the input is One, then print("One.") --print one end end)
surprised of how many people still use deprecated events