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

How can I compare two things in an if statement?

I want to use the equivalent to 'or' when using an if statement, comparing it to one of two things but unsure of how I would do this.

My code basically uses a for loop to shift through an array, but I want it to see whether any of the two items have been clicked, causing another GUI to become visible. However, when i use the 'or' it doesn't seem to work and when any of the buttons are clicked the GUI still becomes visible. Any solutions to this? Here is my code:

for i,v in pairs(foodCatagories) do
	v.MouseButton1Click:Connect(function()
		if v.Name == "Main" or "Sides" then
			step2.Visible = true
		end
	end)
end

1 comment

0

do.... if v.Name == "Main" or v.Name == "Sides" then

1 answer

✓ Accepted answer

Logic in Lua, while very understandable (Using "or" and "and" instead of && and || like in other languages), still can't just be read like normal english. This is because like in math, logic equations both need to be in terms of true/false and follow an order of operations.

For example, solve this: (22 + bannana) * 102 - lol. That doesn't make sense, does it? In math, we need numbers and only numbers. Same thing for logic equations, except instead of numbers we need booleans, which are just special data types that can be either true or false (In the same way a number can be 1, 2, 3, 4... etc.). The special thing about booleans and logic equations, however, is that by using specific operators (Like '+' and '-' in math) we can use almost any arbitrary data as an input to the equation. The operator for this is usually '==', which checks for equality. For example:

local boolean1 = "String1" == "String1" --boolean1 will be true because "String1" is equal to "String1"


local boolean2 = "String1" == "String444" --boolean1 will be FALSE because "String1" is NOT equal to "String444"

This is cool because we can convert strings (As well as other types of data) into a true/false value, which can be used in an if or while statement to control flow.

What's also important here is that, without that '==' converting our strings to booleans, a string is just a string. Let's run through a complex example boolean problem that has an error. (I've used numbers, but you could also use strings or anything else)

local mysteryBool = (input == 1 or 2 or input > 100) 

Let's assume input is 1. How does this solve out?

(1 == 1 or 2 or input > 100) --1==1 is true
(true or 2 or input > 100)  --input > 100 is false
(true or 2 or false) 
--...?

There's a problem here. Now we're trying to compare true and 2, which doesn't make any sense. That's because we never converted 2 to a boolean using ==. Here's the problem rewritten correctly.

local mysteryBool = (input == 1 or input == 2 or input > 100) 

Let's solve it, again using 1 as input.

(1 == 1 or 1== 2 or input > 100) --1==1 is true
(true or  input == 2 or input > 100)-- 1 == 2 is false
(true or false or input > 100)  --input > 100 is false
(true or false or false) --true or false is true
(true or false) -- true or false is true
(true)

And there you go, it works.

In conclusion, remember to always convert your data to booleans before trying to compare with "or" or "and". When in doubt, try to solve through your logic equations using a test value.

--Edit: Changed capitalization of Lua

6 comments

0

If you are using lua ternary operators, i would suggest against converting to booleans.

1

Another notion I want to bring up, you can use non-boolean values inside relational operators, only false and nil will appear to be falsey values, while everything else would appear to be a truthy value

0

Otherwise good tutorial :)

0

Thanks for the feedback!

0

Lua is not an acronym, change LUA to Lua, and no type coercion is done here, strings don't just magically become bools, a == b, a > b, etc simply evaluate to true or false.,

0

The point of my answer was not to explain every nuance of bools, it was to explain them in a simple, easy to digest way. Sometimes it's necessary to sacrifice some accuracy for clearness. You are right about "Lua" though, I always use the wrong form :P

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