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

Is UserInputService and if inputObject.KeyCode inefficient?

Asked by 4 years ago

Take this code for example:

game:GetService("UserInputService").InputBegan:connect(function(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.R then --Also, could be written as [[inputObject.KeyCode == "R"]]
        print("R was pressed")
    end 
end)

From what I understand, every time a key is pressed, this function will fire and determine if the key pressed was R, and if it is, print that "R was pressed". My question being;

can the function only fire if the R key is pressed?

would it still be required that every input is listened to?

Does listening to every input not really matter in terms of lag?

2 answers

Log in to vote
0
Answered by 4 years ago

You're also able to create dictionaries of Enum.KeyCodes if you want to skip if-statements altogether if you're that keen on efficiency. There's no other way to listen for a specific key to my knowledge.

Ad
Log in to vote
1
Answered by
iOwn_You 543 Moderation Voter
4 years ago

It’s not inefficient, this is the best way you can go about listening for keys, it will fire with every key press and thats why you need the if statements but its impossible to listen to a specific key on roblox, so this is the best way to go about it.

Answer this question