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.
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”
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.
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.
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
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.
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?
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.