Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Is there a better way to use if to check same thing?

Asked by
Vezious 310 Moderation Voter
8 years ago
Edited 8 years ago

Is there a better way to use if to check more than one thing....?

Like: I do:

if string == "taco" or string == "pizza" or string == "duck" then

better way:

if string == ('taco'or'duck') then

or something similar?

1
You would use a switch/ case select but Lua does not have that functionality. http://lua-users.org/wiki/SwitchStatement User#5423 17 — 8y

1 answer

Log in to vote
3
Answered by 8 years ago
Edited 8 years ago

Not really.

There's not really a better way. However, if there are a ton of string you would like to check, you could loop through a table and return if the string matches any of the values stored in the table.

Example,

local strings = {
    "taco";
    "cheese";
    "men";
    "women";
    "happy";
    "sad";
    "pyrondon";
    "is";
    "weird"
}

function check(string)
    for i,v in pairs(strings) do
        if string == v then
            return true
        end
    end
    return false
end

if check("men") then
    print("Returns True")
end

That would only really be practical if you have tons of strings you want to check.

Good Luck!

Warning, there might be a better way xD
0
Well dang. Thanks for this though, could probably use it if I have like 20 or more strings. Lol Vezious 310 — 8y
Ad

Answer this question