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 5 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:

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

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

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.

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 — 5y
Ad

Answer this question