Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Which 'if' Statement Is More Efficient?

I am just wondering which statement is more efficient and recommended to use. I don't care about the code, I just want to know for future reference which way is better.

1:

if game.Players[spawnNPC.Data.Owner.Value].Days.Value < 3 then
		foodToChoose = tier_1_tab
	end
	
if game.Players[spawnNPC.Data.Owner.Value].Days.Value < 5 then
	foodToChoose = tier_2_tab
end

2:

if game.Players[spawnNPC.Data.Owner.Value].Days.Value < 3 then
		foodToChoose = tier_1_tab
elseif game.Players[spawnNPC.Data.Owner.Value].Days.Value < 5 then
	foodToChoose = tier_2_tab
end

1 answer

✓ Accepted answer

The 2nd one. But you would have to check the higher numbers first,

if game.Players[spawnNPC.Data.Owner.Value].Days.Value < 5 then
        foodToChoose = tier_2_tab
elseif game.Players[spawnNPC.Data.Owner.Value].Days.Value < 3 then
    foodToChoose = tier_1_tab
end

So basically, if that value is higher than 5, no need to check for 3. If its not higher than 5, then check the next statement.

1 comment

0

Also if you want the 5 to be included, put <=. How your statement is right now it's 6 or higher. Unless thats how you want it.

This discussion is preserved as it was. New answers, comments, and votes are closed.