A bit confused on how Parenthesis work in code Based on this Situation!

Hey! So I have some code here:

-- 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! ()

5 Likes

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.

3 Likes

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

5 Likes

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.

3 Likes

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 🙏🏾!
2 Likes

This returns true if the key is ButtonY and Enum.KeyCode.E if the key is anything else.

If you want to shorten your if statement you can use the .Name property of enums:

local key = Input.KeyCode.Name
if key == "ButtonY" or key == "E" then
	--run code
end
1 Like

Ah so it works with the parentheses around all of it! That’s cool!

Yea using Input.KeyCode.Name is pretty handy too :joy:, makes it even shorter!

local key = Input.KeyCode.Name
if (key == "ButtonY" or "E") then
	--run code
end

Ima use local KeyName = Input.KeyCode.Name! But just to be safe: local KeyName = Input.KeyCode and Input.KeyCode.Name

Actually just searched it up, Input.KeyCode should always return true like mouse click would be “Unknown”

1 Like

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".

1 Like

Oh dang man! That’s good to know! Yea I should just stick to key == "ButtonY" or key == "E".

I get it now!
:pray:t5: thanks!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.