I will break down each line.
local function getNameFromKeyCode(KeyCode)
This line is simply the function name. It takes a KeyCode as an input.
local sk = tostring(KeyCode)
This line is setting the variable sk to a string version of the input KeyCode. This works by using the functions tostring(), which for this purpose outputs Enum.KeyCode.[KeyCodeHere].
local keyCodeName = string.sub(sk, 14)
This line sets the variables keyCodeName to the digits 15+ of string sk. This is done using the string.sub() function, which outputs a string starting from one point, and ending at another. For example, I could take string abcdefg and use string.sub() on it like this:
local output = string.sub("abcdefg", 2, 5)
In this example, output is equal to bcde.
print(keyCodeName)
This line simply prints the variable keyCodeName which should be the name of the key.
return keyCodeName
Returns the variable keyCodeName to whatever calls the function.
end
The end statement.
EXAMPLE:
I will quickly show an example of how this would work.
The function takes KeyCode’s as an input. KeyCode’s are expressed through Enums. So the KeyCode for the Left Shift key is Enum.KeyCode.LeftShift. When running the tostring() function on that, it returns that as a string "Enum.KeyCode.LeftShift", so using string.sub() you can get all of the text following “Enum.KeyCode.”, which just leaves the input, in this case it’s “LeftShift”. That is what get’s printed, and returned (and in the whole script, gets eventually displayed on the UI)
An example script using this function:
local function getNameFromKeyCode(KeyCode)
local sk = tostring(KeyCode)
local keyCodeName =string.sub(sk, 14)
return keyCodeName
end
print(getNameFromKeyCode(Enum.KeyCode.B))
The Output:
18:18:03.437 B - Client - LocalScript:7
Sidenote: This is being run on the client, as you can see in the Output, but the function uses nothing that is client-specific, so you could run it on the server if you wanted to (though inputs, and KeyCode, are usually dealt with primarily on the client).
If you have any extra questions feel free to ask them.