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

Will an if statement be able to make a decisive decision w/ values that involve parentheses?

This is like an extension of my past question, but I'd like to expand on it further.

As the title suggests, I'm wondering if an if statement will be able to make a decisive decision when faced w/ parentheses when handling values; a bit confusing, so I'll provide code for what I mean:

local ExampleVariable = nil
local CompareNumberValue = 1

if CompareNumberValue == (ExampleVariable or 1) then -- This is what I mean
	print('It worked! :O')
else
	print('Didn\'t work :/')
end

In the past, and still present to this day, when you use multiple or's but don't repeat the value, it doesn't work:

local ExampleNumberValue = 0

if ExampleNumberValue == 1 or 6 or 8 then -- Obviously will fire, b/c 6 & 8 will return true, and aren't being compared to anything
	print('Waah...?')
end

But w/ my raised question, will an if statement make a decisive decision when faced w/ parentheses?

EDIT

Well, just to be safe & ask, is this good practice to use? Or a good solution to some problems? And why would it as if value = var or num, the literal meaning for a sentence, instead of if value = var, then true, else num is true?

2 comments

0

Why ask the question if you can just try it out.. Gees people..

0

Well, I guess if someone else had a question similar to this, they could look up & see it.

3 answers

✓ Accepted answer

Parenthesis direct order-of-operations, but they don't impact the "meaning" of your code.

If you have code like

if foo == (bar or 1) then

this is of course the same as

local barp = bar or 1
if foo == barp then

I would recommend not embedding or and and used in this way in larger expressions.

It can easily get confusing.

Instead, save the result to a variable.

message = message or ""

I think you will find it is rather uncommon to need these inside conditions, though.

There are only really two times I ever use or outside of using it on actual booleans:

  • defaulting a parameter from a function
function foo(blah)
	blah = blah or 0
	print(blah)
end
  • defaulting an access from a table
local count = wordCount[word] or 0

For most other cases I would prefer an actual if.

3 comments

0

So I'd want to use this only for special cases, like the examples you gave? And it'd be better to do this for a variable, rather than have it all be in an if?

0

Yes. That recommendation is for humans, Lua doesn't care. But doing too much in a single line can make it impossible for a human to get what you're trying to do.

0

Alright, tyvm for your answer & explanations. :)

I believe so. ExampleVariable and 1 are contained within the parentheses, which should technically make it its own statement. Therefore, (ExampleVariable or 1) should always return 1.

EDIT: This is generally the way I would write my code:

local level --no need to declare, we want it nil for these purposes
local defaultLevel=1 --if there's no level, we still want something to work with.
local levelToUse=(level or defaultLevel) --get at least 1 value that isn't nil
if levelToUse==1 then --use our value that is guaranteed to be a number
	print("User is on level 1") --print to output
end --close the statement

4 comments

1

Is it good practice to use this kind of method?

0

I generally dislike using long lines of code, as they can appear messy and hard to read, but it's really up to your discretion.

0

How could this be shortened then? I can't see how. e-e

0

I edited the answer, you can see it there.

Eh, if you say "or" it will only check the value after or if the previous value before or was issued as false, and also why do you have to ask? when you can experiment yourself...

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