Avoid using else / elseif - Make your code smaller and more efficient (For beginners)

He’s just using them for type checks.

1 Like

TYSM, I was wondering because I saw it before, but never had the oppurtunity to ask. Have a great day!

1 Like

It might not be useful to always use tables in place of if/else(if) statements. I find if/else(if) statements to be more efficient, but for Something like this:

Tables are useful, and you could have shortened the table to a single line:

local NumberTable = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"}

And as mentioned by someone above, villainizing if/else(if) statements to be portrayed as completely useless is absolutely wrong. Both ways have their use cases. And making code shorter does not always means its efficient (as mentioned by someone else above).

Other than that, It seems fine.

1 Like

Here’s my example on what I am trying to replace. I have a small elseif loop that goes through some conditions but for the last 2 I need it to check what the returned value is from table.find and if it equals another value it’ll replace it with the appropriate name. Someone told me it wasn’t very efficient so if anyone knows how to fix it, it would be appreciated!

		local Positions = {
			Left = {
			[1] = LOne.Position,
			[2] = LTwo.Position,
			[3] = LThree.Position,
			[4] = LFour.Position
			},
			Right = {
				[1] = ROne.Position,
				[2] = RTwo.Position,
				[3] = RThree.Position,
				[4] = RFour.Position
			}
		}

			if Team == "Team1" then
				Lc = T1.Position
			elseif Team == "Team2" then
				Lc = T2.Position
			elseif Team == "Team1Pad" then
				Lc = if table.find(Team1, Char) == 1 then LOne.Position elseif  table.find(Team1, Char) == 2 then LTwo.Position elseif table.find(Team1, Char) == 3 then LThree.Position else LFour.Position
			elseif Team == "Team2Pad" then
				Lc = if table.find(Team2, Char) == 1 then ROne.Position elseif  table.find(Team2, Char) == 2 then RTwo.Position elseif table.find(Team2, Char) == 3 then RThree.Position else RFour.Position
			end