Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

I know how to do onKeyDown, but how can you do it using Enter?

Asked by 8 years ago

I want to make it do something when you press Enter, but if you do onKeyDown with strings, you can't make a string version of Enter, and using numbers, I don't know the number for Enter.

0
Enum.Keycode.Enter or something? User#11440 120 — 8y
0
You could always look up the ASCII representation of the enter key, but you shouldn't be using key events from the player mouse. UserInputService is much better. ScriptGuider 5640 — 8y

1 answer

Log in to vote
2
Answered by 8 years ago
Edited 8 years ago

ASCII

ASCII (or American Standard Code for Information Interchange) is an encoding system in which English characters are converted to decimal numbers (referred to as their byte codes), so machines can convert those decimal numbers to machine code (binary). You can look at all the ASCII characters in this table.

Why should you know this?

Just to make things clear, you don't have to know every single number that corresponds to every single ASCII character. In fact, we have functions that can make this conversion for us in Lua's string library, called char and byte. Using string.char will convert any byte code (ASCII encoded character), back into plain text. string.byte on the other hand, takes a string in plain text, and converts it to it's ASCII representation.

Implementing this knowledge

The reason I'm explaining all of this, is because when you use byte (either from the string library, or just as a method on a string), what you're doing is converting that character to ASCII. So to answer your question, you'd compare 13 to the byte code of the key pressed, since 13 is the Enter key in ASCII.

BUT WAIT!

Even though this solves your current problem, there's still more to consider. ROBLOX has integrated a service called UserInputService for quite some time now, and it is the preferred method of collecting input from a user. Input collected by the PlayerMouse shouldn't be used anymore.

I highly suggest you switch from using the PlayerMouse for input, to UserInputService. It may take some tampering with to get used to, but it's a lot better and you'll enjoy using it once you get the hang of it. Anyway, hope this helped, just let me know if you have any questions.

Ad

Answer this question