Issue about conditional structures

  1. What do you want to achieve?
    I want to reach the block of code after the “else” if any of previous conditions fail

  2. What is the issue?
    script do not run the rest of code if the “hit” is true.
    it will just verify the conditions inside it and jump to the end of the script(not running the “else”)

image

Use lua’s elseif construct.

if condition1 == true then
--dosomething()
elseif condition2 == true then
--dosomething()
elseif condition3 == true then
--dosomething()
else
--dosomething()
end

EDIT:

You’re just not nesting it correctly, that’s all.

Move the else block to inside the first condition.

Could you not put the conditions 2 and 3 within the first condition, as such?

if condition1 then

   if condition2 then
   -- code for condition 2

   elseif condition3 then
   -- code for condition 3

   else
   -- if condition 2 and 3 are true then do this

Hopefully I’ve understood your question correctly!

use or

in if statements you can chain conditions with or, and, not, etc.
for this you can use:
if condition1 == true or condition2 == true then
this will return when condition1 or condition2 are true.
To put it in your code just go

if condition1 == true or condition2 == true or condition3 == true then
--if condition 1,2, or 3 are true
else
--your code
end
1 Like

i need to verify if all conditions are true, not only one
and the second condition can only be checked if the first is true.
image
because “hit” cannot be nil

for the rest i can use

if condition2 and contdition3 then
--
end
1 Like