TL; DR: "Else if" and "elseif" result into completly different behaviour. Use "else if" as default and only use "elseif" in special cases, rather than the other way around.
#1 You maybe expect it to print both "YaY" and "Okay?"
1
2
3
4
5
6
7
8
9
10
11
local a = 5
local b = "hello"
if (a == 5) then
print("YaY")
elseif (b== "hello") then
print("Okay?")
end
Prints "YaY" - If "a" were anything but 5, it would print "Okay?" (if b remains "hello")
#2 Exactly the same code without elseif - it is more noticeable that it only can print 1 thing at max
1
2
3
4
5
6
7
8
9
local a = 5
local b = "hello"
if (a == 5) then
print("YaY")
else
if (b=="hello") then
print("Okay?")
end
end
Prints "YaY"
#3 I want to print both; and both variables a&b dont have any connection with each other anyway
1
2
3
4
5
6
7
8
9
local a = 5
local b = "hello"
if (a == 5) then
print("YaY")
end
if (b== "hello") then
print("Okay?")
end
Prints "YaY" and "Okay?"
#4 I want to print specifically when both conditions are true - thus have a connection (example: AND Gate)
1
2
3
4
5
local a=5
local b="hello"
if (a == 5 and b=="hello") then
print("SUPER")
end
Prints "SUPER"
#5 AND GATE example written differently
1
2
3
4
5
6
7
local a=5
local b="hello"
if (a==5) then
if (b=="hello") then
print("SUPER")
end
end
Prints "SUPER"
#6 I want to print both with using elseif -> this code is more ugly than the others for obvious reasons (dont do it this way)
1
2
3
4
5
6
7
8
9
10
local a = 5
local b = "hello"
if (a~=5) then
print("super abstract example")
elseif (b=="hello") then
if (a==5) then
print("YaY")
end
print("Okay?")
end
Prints "YaY" and "Okay?" - Lua had to check 3 values (slow)
#7 modified #6 code for less checks:
1
2
3
4
5
6
7
8
9
10
local a = 5
local b = "hello"
if (b=="hello") then
if (a==5) then
print("YaY")
else
print("super abstract example")
end
print("Okay?")
end
Prints "YaY" and "Okay?" - Lua had to check only 2 values (better)
I hope the difference between "elseif" and "else if" is clear now.
Homework: Do the code examples #6 and #7 have any differences when the inputs variables a or b get modified?
In my eyes, "elseif" is best used in a scenario when you want to check
only 1 variable for different values.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function myServerstatus(scenario)
if (scenario==1) then
strongMobSpawn()
unlimitedHealth()
print("Strong Mobs + Unlimited Health")
elseif (scenario==2) then
unlimitedAmmo()
mobSpawnOnlyInBeginning()
print("Unlimited Ammo + Mob Spawn in the beginning")
elseif (scenario==3) then
peacefulMode()
reduceAmmoTo(64)
print("No Mobs + Limited Ammo")
elseif (...) then
...
else
serverShutdown()
print("Unknown scenario! Server is sudo shutting down - yes yes, bang bang")
end
end
This is what a switch-case expression looks like in other programming languages
Many things like "elseif" and "and" are
shortcuts (with maybe slight changes). This is a disadvantage when you are new and learning. I think, it is always important to learn and understand the basic concepts first. Sadly, those segments (shortcut vs. necessary) are not highlighted enough (for newbies aka learning the first programming language) in my eyes.
For people that learned real programming languages C# or Java first and then Lua second, those shortcuts are second-nature and often overlooked.
Real programming languages like C# or Java intentionally removed "elseifs" and introduced "switch cases" to prevent confusions like in your script. (Switch-case limits itself to use 1 variable only!)
Just my 2cents, I dont know nearly enough about programming or Lua to give you a
real correct detailed answer and the topic is way too complex to describe it in a single answer (I compared apples with oranges here for simplicity too).
Regarding shortcuts: You will probably run into similiar errors when using lua tables or arrays regarding ipairs and pairs. The difference is subtle but very important (like else if and elseif). But with many other things as well. The more you learn (and understand), the easier advanced topics will get.