Need help with my simple script, please explain like I'm 5

Hello,

I’m new to programming and I’m trying to make a script that will make my character say “hello” when I press the “H” key. I know it’s a very simple script, but I’m having trouble understanding how it works. Could someone please explain it to me like I’m 5 years old?

Here’s what I have so far:

local player = game.Players.LocalPlayer
local character = player.Character
local hKey = Enum.KeyCode.H

function onKeyPress(inputObject, gameProcessedEvent)
  if inputObject.KeyCode == hKey then
    character:SayHello()
  end
end

player.InputBegan:Connect(onKeyPress)

I know it’s not much, but any help would be greatly appreciated!

1 Like

Just to clarify - the script is working and you just want an explanation for it?

Thanks for reaching out! To clarify, I am diabetic and also have some mental health issues. I just wanted to let you know that upfront so you know where I’m coming from.

As for the script, no, it’s not working. I’m trying to make it so that my character says “hello” when I press the “H” key, but nothing is happening. I’m not sure what I’m doing wrong, which is why I’m asking for help.

I know it’s a very simple script, but like I said, I’m new to programming and I’m having trouble understanding how it works. If you could explain it to me like I’m 5 years old, I would really appreciate it.

Simply enough you cant use :SayHello() on a character neither :Say() because they dont exist
but you can you chat service to make a dialog pop on the head so

local player = game.Players.LocalPlayer
local character = player.Character
local hKey = Enum.KeyCode.H

function onKeyPress(inputObject, gameProcessedEvent)
  if inputObject.KeyCode == hKey then
    game:GetService("Chat"):Chat(character:FindFirstChild("Head"),"Hello") -- it gets the chat service and pops a bubblechat on top the head of it saying the text of your choice
  end
end

player.InputBegan:Connect(onKeyPress)

Note that this is only client so nobody else will see the character say hello
you would need RemoteEvents to do it server wise (so everyone see’s it)

Thank you for your response and for explaining the issue with the :SayHello() and :Say() methods. I understand now that those methods don’t exist, and I appreciate your suggestion to use the Chat service instead.

However, I still disagree with your suggestion to move this post to the “Scripting Support” category. I believe that my advanced physics simulation is relevant to this category, and I’m specifically asking for feedback on that topic.

I also wanted to let you know that I have stage 69 cancer, so please be more compassionate and understanding in your communication with me. I’m doing my best to learn and improve, and I appreciate any help and guidance that the community can provide.

Can you please provide more specific advice or resources for working with the physics system in Roblox, or suggest any other ways that I can achieve my goal of making the cart say “hello” when the “H” key is pressed?

i dont really think you can do it in any other way than using chat service

Alright, I’ve started writing a veeerryyyy detailed explanation of your script. However, you’re right, it does contain some errors. The one being the error mentioned above, and the other one being that player.InputBegan does not exist. Would you like me to fix those and explain why they don’t work?

Yes, that would be very much aprreciated by my concious brain.

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.

Third line:

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, inputObject and gameProcessedEvent, is because of the InputBegan used later in the script: InputBegan gives you an inputObject and a gameProcessedEvent.

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:

game:GetService("UserInputService").InputBegan:Connect(onKeyPress)

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.

1 Like

this is not very helpful can please try again

Wow, thank you so much for explaining all of this to me! I feel like I finally understand what’s going on in my script. You are such a helpful and patient person.

By the way, I was wondering if you would be willing to be my discord kitten? I’m not sure what that means exactly, but it sounds like a really cool thing to have. And if you’re not interested in being my discord kitten, that’s okay too. I just thought I would ask.

Anyway, thank you again for the amazing explanation. It really helped me a lot!

Sure, by the way. Sounds like great fun.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.