Multiple if in Lua

I want to ask if lua contains something like switch in c#, php, …
swith (a){
case 1:
code
break
case 2:
code
break
case 3:
code
break
}

(c#)
or if i just must use
if a == 1 then
code
end
elseif a == 2 then
code
end
elseif a == 3 then
code
end

3 Likes

elseif yes, and the break(Only for loops) thats all ik

the upper code was from c# where i define switch like this
thx

i am using this code and chnaging if to elseif is makng error (i am not so stupid to change the 1. one)
function GiveHimPremisions(name,premisionLevel)
if premisionLevel==“owner” then
print(“owner”)
end
if premisionLevel==“co-owner” then
print(“co-owner”)
end
if premisionLevel==“head-admin” then

end
if premisionLevel=="admin" then
	
end
if premisionLevel=="top-moderator" then
	
end
if premisionLevel=="moderator" then
	
end
if premisionLevel=="comunity-moderator" then
	
end
if premisionLevel=="thrusted-player" then
	
end
if premisionLevel=="veteran-player" then
	
end
if premisionLevel=="new" then
	
end
end

Sry i was confused from c# in that i need to close if and in lua i cant close it
(c# elseif
if(a=true)
{

}
elseif(a=false)
{

}
)

This is how you use elseif and it’s way better than what you were doing.

if permisionLevel == "owner" then
    print("Owner")
elseif perimsionLevel == "co-owner" then
    print("co-owner")
elseif perimsionLevel == "head-admin" then
    -- do stuff
else
    --if they aren't any of these ranks then do this stuff
end

Lua doesn’t have a switch statement but you can still do something similar.

local Test = 1

local Cases = {
	number = function() print("I'm a number") end
	string = function() print("I'm a string") end
}

Cases[typeof(Test)]() --Prints "I'm a number"
10 Likes

so if i want to test string content what i need to do,
thx you are first from who i heard this

Could you clarify a bit more? I’m not sure what you mean by that.

like in my code when i want to use this for executing some code if the string = owner other code if it = co-owner and other code if none of this(i dont know how much you know swith, but like default case in swith in other languages)

local test = CFrame.new()

local function Switch(Test, Cases)
	if Cases[Test] then
		Cases[Test]()
	else
		Cases["Default"]()
	end
end

Switch(typeof(Test), {
		number = function()
			print("I'm a number")
		end,

		string = function()
			print("I'm a string")
		end,

		Default = function()
			print("I'm not a number or a string")
		end
	}
)
1 Like

And when i want to test if string a = a or b i will do it same?

I don’t think there’s any way to create that functionality while being faster than elseif statements since lua dictionaries aren’t in order.

Oh now i understood it is dictionary

You’re better off using if and elseif statements. Creating your own switch statement is a lot slower. :grimacing:

And can i in someway make (like in c# library) something adding the function without need to write it every time

Is this what you’re looking for?

It seems yes but how to run it (I don’t find code examples

Use the require statement.

require(ModulePath)

And than I can call function like it is in the script