As YBN_oSy said, you need to use the UserInputService
, press here or here to see more tutorials about UserInputService
.
As I can see, you are new to Roblox Lua Scripting, so I will teach you the diference between a LocalScript
and a Script
, so you can understand why this code MUST be in a LocalScript
.
A LocalScript
is a client-side code. What does this mean? This code runs in the client connected to Roblox (the player). This type of code can only run in certain locations and is used to access some client-only objects, for example, the player's keyboard. For more information about LocalScripts
click here.
A Script
is a server-side code. This means that this code runs in the game server. This code can run everywhere and can access some server-only objects, such as ServerStorage
.
Now that you know the Roblox scripting basics, we can start with the code:
3 | local UIS = game:GetService( "UserInputService" ) |
Okk, now that we have the UserInputService
stored as a local value, we are going to get the LocalPlayer
.
3 | local plr = game.Players.LocalPlayer |
Now let's do a code that prints the name of the player when they press a Key
1 | function KeyPressed(press) |
2 | local key = press.KeyCode |
3 | if key = Enum.KeyCode.E then |
8 | UIS.InputBegan:Connect(KeyPressed) |
This code should print the name of the player that presses E. Put the code in a LocalScript
inside StarterPlayerScripts
.
The next time you post a question in the scriptinghelpers page, make sure that you give a detailed description of what you want to do, the code you tried and what is wrong with your code.
Sorry if my English is bad, but I am not from a English country.
I hope I helped. For more Roblox Scripting tutorials visit Roblox Studio Wiki, RobloxDev or the scriptinghelpers guide.