Need explanation on what keybytes are

I was reading through an open-sourced sky-diving script when I came across this:

Player:GetMouse().KeyDown:Connect(function(Key)
	if Key:byte() == 119 or Key:byte() == 17 then -- W
		TiltFB = -1
		GlideFB = 2
		Drag = DragRatio*(0.997+DragDiminish)
		if IsFalling.Value == true then
			FreeFallAnimationForward:Play(0.25)
		end

I’ve never seen anything related to “Byte” before in scripting, what is it? What are the numbers? Do they detect certain keypresses? If so how? Is it only exclusive to KeyDown? So many questions, so little answers.

From what I can remember, a byte is a succession of (typically) 8 bits used to display a value. Bits are single binary digits, either a 1 or a 0.
A byte can portray 256 unique values (2 to the power 8 or 2^8).

Also, this may not be true, but a friend once told me:
Lua strings are explained on a per-byte level, meaning you can make use of the full 0-255 range, and a Lua string will keep that information and read it back as singular bytes.

1 Like

The Key argument passed by the Mouse.KeyDown event is a string representing the key that was pressed. byte is a string method that gets the ASCII value of a character.

This table shows that 119 is the ascii value for the lowercase “w”

1 Like

Yep, they are numbers that in this case represent the 01001 that the keyboard sends to the computer to represent characters like “w” and yeah when this key is equal to 119 that represents “W” from the Keycode Enum documentation.

The KeyCode Enum contains a list of Byte keycodes that represent the button used in a user input.

Note, KeyCode keyboard values refer to the physical position of buttons on a standard QWERTY keyboard. This means that, provided the user’s system is configured correctly, the location of keys (such as WASD) will remain the same on other keyboard types, such as Dvorak keyboards (where it would map to ,AOE).

Also it’s not exclusive to keydown, you can also use the newer super UserInputServices to obtain the keycode through the input object property returned by the InputBegan and InputEnded.

Is byte used to get code that represent each letter on the keyboard? What does this have over enum.KeyCode?

So what’s the difference between this and keycode.Letter? Wouldn’t it be easier to just put down the letter than looking up the code to the letter?

Enums are generally more efficient, though I assume Mouse.KeyDown returns a string instead of an Enum. Because of this, they used the byte function to compare the strings.

Yeah the :Byte() function is the same as string.byte as @blokav mentions. It’s just syntax the same way we use : in functions.

From the lua pil.

An expression like o:foo(x) is just another way to write o.foo(o, x) , that is, to call o.foo adding o as a first extra argument. In Chapter 16 we will discuss such calls (and object-oriented programming) in more detail.

local s = "ABCDE"
s:byte(3,4) -- can apply directly to string variable

http://lua-users.org/wiki/StringLibraryTutorial

For the question of why not just use an Enum? I believe it may be due to historical reasons before UserInputServices existed but yeah it’s easier and more readable hence efficient to put down the Enum code and it should work for that specific letter.

Mouse doesn’t use the KeyCode enum. It just passes the letter of the key pressed as a string.

UserInputService is preferrable since a lot of the mouse functions and events are deprecated.

I looked up the codes and 119 is indeed for “w” but I can’t find one for 17 which is supposed to be “w” as well I assume, what’s up with that?
Does each letter have multiple code?

Yeah each letter has multiple codes, you can search it up here though it’s more of a computer science topic.

According to it 17 is:

ASCII control characters non printable

ASCII code 17 = DC1 ( Device control 1 )

Not exactly sure what the scriptwriter was thinking but yeah 17 doesn’t really matter in that code snippet unless you can somehow input device control 1 somehow.