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

Which 'if' Statement Is More Efficient?

Asked by 6 years ago

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:

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

2:

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

1 answer

Log in to vote
1
Answered by
dirk2999 103
6 years ago

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

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

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.

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. dirk2999 103 — 6y
Ad

Answer this question