Okay, so let’s take a look at your script:
The first line is the following:
local player = game.Players.LocalPlayer
You created a variable called ‘player’. Then, you took advantage of using a LocalScript: Since a LocalScript only works for the “local” player (in other words, only for that one person that is currently running the script), you can find “yourself” by navigating to the “Players” service, where all players are located, and then looking for the “LocalPlayer”.
Now, you can call this variable to reference the player. This line is fine and works as expected.
The second line:
local character = player.Character
Now, you’re defining a variable,
character, and assigning the player’s Character to it. Straightforward!
Here too, you can use the character variable to reference the player’s character.
local hKey = Enum.KeyCode.H
Again, you’re defining a variable (
hKey) and assigning the H-button to it. The side on the right of the equals sign possibly looks very complicated for you, but to explain it very simply, it’s essentially the way you can tell the script that you want the H-key on your keyboard. The script can only understand it this way, there is no simpler way.
Now, out of the variables and in to the “actual” code:
function onKeyPress(inputObject, gameProcessedEvent)
Now you’re defining a function and you’re naming it “onKeyPress”. In the parenthesis, you tell the script how many “arguments” the function has (this means how many things the function accepts when you call it). The reason why you have these two arguments,
gameProcessedEvent, is because of the
InputBegan used later in the script: InputBegan gives you an
inputObject and a
Then, the if statement:
if inputObject.KeyCode == hKey then
An if statement checks whether what you tell it is true. If it is true, it runs the code inside of it. Here, you’re checking whether what the user pressed (which is what you can look at when you do
inputObject.KeyCode) is equal to the H-key. In other words, you’re checking if the user pressed the H-key.
Now, the next line is wrong, since there isn’t a function on the character so that you can do
character:SayHello(). Sadly, there is no simple solution for it, since what you actually want it to do is way more complicated and it might not be fun to look at something you completely don’t understand. Maybe try doing something else, such as colouring the baseplate in a random color, or maybe go even simpler and just make it print something.
The next two lines are just
ends. The first indicates that the if statement is finished and the second indicates that the function is finished.
Now, the next line is wrong, too. Something such as
player.InputBegan does not exist. Instead, you’re going to have to use a service called “UserInputService”. The UserInputService is the service in Roblox that handles all user input, as the name says. Using the UserInputService, you can do this:
What the .InputBegan:Connect(onKeyPress) tells the script is that whenever the user presses anything on their keyboard, you want to run the function you defined above, called “onKeyPress”, with the key or whatever that they pressed.
Hope this helped.