Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

am i using the "Or" operator in an if statment properly?

Hi, i am confused on the syntax for and "Or" in an if statement and it is causing my code to error

if inputObject.KeyCode == Enum.KeyCode.W or inputObject.KeyCode == Enum.KeyCode.A or inputObject.KeyCode == Enum.KeyCode.S or inputObject.KeyCode == Enum.KeyCode.D and isGrabbing  == true then

end

4 comments

1

Your error...?

0

Is correct

0

20:40:15.995 - Workspace.Code1400.PickUpHandlerClient:114: attempt to index nil with 'Parent'. Its weird it because it doesnt reference the if statment. when i press W the code will be run even though the "and's" value hasn't been met.

0

and the code runs fine if i don't have the "or" operators in it

1 answer

✓ Accepted answer

It doesn't seem would occur an error to me, but I still cleaned up your code a little bit.' Hope it can work.

local inputKeyCode = inputObject.KeyCode
if (inputKeyCode == Enum.KeyCode.W or inputKeyCode == Enum.KeyCode.A or inputKeyCode == Enum.KeyCode.S or inputKeyCode == Enum.KeyCode.D) and isGrabbing  == true then
--um
end

P.S. If the isGrabbing is a BoolValueObject, then it should be isGrabbing.Value == true.

4 comments

0

isGrabbing was only a variable but i fixed the problem. turns out that since the "Or" was after the initial condition the if statement didn't read the "And" at the end because the first Condition was already met, thank you for your time though!.

0

Yes, exactly. Brackets are way more stronger than it sounds!

1

Oh i see how brackets would have done it. i never knew thought of that so i ended up putting the "And" in a new if statment. thanks ill make sure to use that in the future when dealing with multiple operators or conditions : D

0

:D

This discussion is preserved as it was. New answers, comments, and votes are closed.