How to do and/or statement in lua?

  1. What do I want to achieve? and/or statement?

  2. What is the issue? I don’t know to do an and/or statement in Lua.

  3. What solutions have you tried so far? Googling, Found nothing.

Does anyone know how to do an and/or statement? For example:

Code:

elseif (string.match(v.Parent.Parent.Name)) == "dark" and/or --[[ Just an example, I don't know how to do this.]](string.match(v.Parent.Parent.Name)) == "v4" then
1 Like

and requires the statement to the left and right of it to be true
or requires just one statement to the left or the right of it to be true
So it would look like this

elseif (string.match(v.Parent.Parent.Name)) == "dark" and (string.match(v.Parent.Parent.Name)) == "v4" then

but another thing unrelated to this post is your not using string.match correctly, im assuming you’d want it to be more of this with string.find instead

elseif string.find(v.Parent.Parent.Name, "dark") and string.find(v.Parent.Parent.Name, "v4") then

I think what you could do is just do an or statement. There’s really no way else of doing an and/or statement, since lua doesn’t really support it to begin with. The / is a division operation, and because it’s an operator sign, if pop and/or corn then. Because, again, the back slash is an operator sign.

But a way you’d still do and/or statements is just to check to see if it meets either condition, with an or statement:

local boxID = "JD4210321PPD2"

if boxID == "MX9420193BZ9" or boxID == "FP6572910QOM0" then
     print("boxID")
end

What this would do is just check if boxID matches the first ID in the condition. And if the first statement isn’t true, it’ll fall back to the second condition to see if that’s true. If that’s not true it’ll just keep falling back to the other conditions until either one is true or all of them is false.

You could do something else as well to do an and/or type of statement, but it wouldn’t be as optimized in said script and is just more lines inside of your script.

local berries = 36
local peaches = 9

if berries <= 30 then
-- code, code, code...
elseif peaches >= 13 then
-- code, code, code...
elseif berries >= 6 and peaches <= 9 then
-- code, code, code...
end 

Lets say a Value1 has stored true in it and Value2 has stored false in it.

and:

If Value1 == true and Value2 == false then
print("Yes") -- This will print `Yes` because Value1 is true and Value2 is false.
elseif Value1 == true and Value2 == true then
print("No") -- This will not print, because Value2 is not false.
end

or:

If Value1 == true or Value2 == true then
print("1") -- This will print "1" because Value1 is true.
elseif Value1 == false and Value2 == false then
print("2") -- This will print "2" because Value2 is false.
elseif Value1 == true and Value2 == false then
print("3") -- This will print "3" because Value1 is true or Value2 is false.
end
local val1 = true
local val2 = false

if val1 and val2 then end -- This is false, because both of them are not true.
if val1 or val2 then end -- This is true, because at least one of them is true.

if 5 == 5 then end -- This is true, because 5 is equal to 5.
if 4 < 5 then end -- This is true, because 4 is less than 5.
if 4 > 5 then end -- This is false, because 4 is not greater than 5.
if 5 <= 5 and 4 <= 5 then end -- This is true, because 5 is equals to 5 and 4 is less than 5
if 4 >= 4 and 5 >= 4 then end -- This is true, because 4 is equals to 4 and 5 is greater than 4

It seems that you’re confusing or with exclusive or. Exclusive or means one or the other, not both. Though or sometimes works that way in English, in computer science or means one or the other or both.

Oh, I could just use an elseif statement inside an elseif statement…

local string = "hi hello"

elseif tostring(string) == "hello" and tostring(string:find("hello")) == "hi" then
print("yes")
elseif tostring(string) == "hi" or tostring(string:find("hello")) == "hello" then
print("yes")
  1. Please don’t use tostring() when the variable is already a string, that function is only used to convert a number to a string.
  2. No, you cannot use an elseif statement inside of an elseif statement, and you also cannot use an elseif statement if there’s no if statement, therefore the code wouldn’t work.

oh yeah I forgot the conditions of tostring lol

ok use an if statement inside an elseif statement

local string = "hi hello"

if string then
elseif string == "hello" and string:find("hi") then
print("yes")
if string == "hi" or string:find("hello") then
print("yes")
end
end

Yeah you don’t need either of those. Or already does what you need it to do. A or B is the same in programming as A and/or B in English. If that’s not working, you have a different problem.

oh I was just confused and unconscious lol

2 Likes