-- Inside InputBegan Function
if Input.KeyCode == (Enum.KeyCode.ButtonY or Enum.KeyCode.E) then
end
And only the ButtonY on my Xbox Controller works with the parenthesis! It works perfectly without the parentheses and separated:
if Input.KeyCode == Enum.KeyCode.ButtonY or Input.KeyCode == Enum.KeyCode.E then
end
But it would make sense to add parenthesis with the “or” choices, it seems like it should help the script but it just makes it so you can’t select Enum.KeyCode.E.
How come the parenthesis don’t work around the “or” choices? Shouldn’t the parenthesis return one of the values if the other is not valid?
This is not a big deal, but I’m curious because I thought I understood when to use parenthesis! ()
Putting something in parentheses essentially separates it from the rest of the equation or comparison. if Input.KeyCode == (Enum.KeyCode.ButtonY or Enum.KeyCode.E) makes sense to us, but the computer sees (Enum.KeyCode.ButtonY or Enum.KeyCode.E) and attempts to compute it as a boolean value, which obviously will not work. I might be totally wrong as I’m not too familiar with how computers read code, but I’d just stick to separating the comparisons like you demonstrated in the second code snippet.
Parenthesis prioritizes conditions, so (Enum.KeyCode.ButtonY or Enum.KeyCode.E) will run first and evaluate to the first condition that is seen as true by lua logic, in this case Enum.KeyCode.ButtonY.
So the entire thing evaluates to Input.KeyCode == Enum.KeyCode.ButtonY which excludes the case of KeyCode being equal to E. Instead, if you want to also check that you should use the second if statement shown in your post.
What you did tho is useful for situations like this:
local char = player.Character or player.CharacterAdded:Wait()
which works as “if the player doesn’t have a character, evaluate the second condition, aka wait for it to be added”.
Actually, lua sometimes doesn’t return boolean values from conditions(in this case it would return Enum.KeyCode.ButtonY) so a cool trick I have figured out is to return not not condition instead to convert it to a boolean.
Ah okay so it is checking if Enum.KeyCode.ButtonY is real, which it most certainly is so it returned true with Enum.KeyCode.ButtonY
What about if the parentheses is wrapped around with Input.KeyCode? Would that work or it will probably be the same?
if (Input.KeyCode == Enum.KeyCode.ButtonY or Enum.KeyCode.E) then
end
``` im on phone, but I’ll try this tomorrow! Or if you could try it that’d be lit 🔥
Thanks Nyrion 🙏🏾!
No that’s not true, the above statement will always run because if a user clicks lets say F then key == "ButtonY" will be false but "E" is always true due to lua logic. The condition (key == "ButtonY" or "E") will always be true no matter the input and it’s not identical to key == "ButtonY" or key == "E".