Well, I saw tip of the day "Writing one function is quicker than writing the same code twice. (Credit to CodeTheorem)" and I remembered a question that I asked to myself before, and I still wonder it. Is it possible to use only one function for multiple keys for keypress events?(Hope typed that right)
To be more explanatory let's say we have a LocalScript which prints another thing as we type another key, let me give an example:
function onKeyPress(inputObject, gameProcessedEvent)
if inputObject.KeyCode == Enum.KeyCode.One then
print("Hello! Whazzup?")
elseif inputObject.KeyCode == Enum.KeyCode.Two then
print("I'm superalp1111")
elseif inputObject.KeyCode == Enum.KeyCode.Three then
print("And this is an example!")
end
end
game:GetService("UserInputService").InputBegan:connect(onKeyPress)
Is it possible to call a function to get string to print from an array to print as player presses a key and we use that KeyCode as an index? Like, you know, converting an Enum.Keycode to a number. Maybe like:
a = {"Hello! Whazzup?","I'm superalp1111","And this is an example!"}
function onKeyPress(inputObject, gameProcessedEvent)
print(a[tonumber(inputObject.KeyCode)]) --Just an example of the thing I want don't rage on me mkay?
end
game:GetService("UserInputService").InputBegan:connect(onKeyPress)

1 comment
Thanks. Others told me how to do it in chat too, thanks to them too :)