• This is a read only backup of the old Emudevs forum. If you want to have anything removed, please message me on Discord: KittyKaev

Alternative to most "elseif" statements

Zadax

Enthusiast
Hiya! As most people know, when you want to create a script that does "X" when "Y" happens but does "W" when "Z" happens. Most people use a LOT of elseif statements, it's handy but it gets pretty messy with a lot of different events

Example:
Code:
function uh(event, what) -- the "0" are just there because it's part of the method itself
if what:WhatEver() == 1 then 
what:Say("Whatever", 0)
elseif what:Whatever() == 2 then
what:Say("Whatever 2", 0)
elseif what:Whatever() == 3 then
what:Say("Whatever 3", 0)
end
end
And so on....

BUT, we can use locals to have a pretty much clean script. You might ask yourself "dafuq" well, I understand you do. We are going to create a pretty much clean list of variables to dont have a VERY huge script with a LOT of "elseif" statements. Now, you might think "omg shut the hell up already and teach us" Well, here is it:
Code:
local Whatever = {
[1] = "Uh, Whatever",
[2] = "Uh, Whatever2",
[3] = "Uh, Whatever3"
}; -- And so on

Now, you may think this "okay, now i know how to create that, but how am i going to use it". Well, it's example time!

Code:
function uh (event)
what:Say("Uh, "..Whatever[what:WhatEver()])
end

Okay, it's done. At this point, you should have this:
Code:
local Whatever = {
[1] = "Uh, Whatever",
[2] = "Uh, Whatever2",
[3] = "Uh, Whatever3"
};

function uh (event, what)
what:Say("Uh, "..Whatever[what:WhatEver()])
end

Hope this was useful for you.
Regards, Zadax.
 

Tommy

Founder
"switch" statements are also good to use when you don't want to have a lot of "elseif" statements. Thanks for the tutorial. \o/
 
Top