Script code isn't running properly when adding more else if statements

I'm using stacking else if statments to use different skills based off of specific variables, for some reason when adding a second if else statement it breaks all the previous ones as well? anyone have any idea why.

The code in question:


var subject = $gameParty.members()[0];

if($gameVariables.value($gameVariables.value(11)) == 0){

$gameMessage.add("Click"); }

else{ if($gameVariables.value($gameVariables.value(11)) == 1) {

subject.forceAction(216, -2);

BattleManager.forceAction(subject);

this.setWaitMode('action');}

else{ if($gameVariables.value($gameVariables.value(11)) == 2){

subject.forceAction(217, -2);

BattleManager.forceAction(subject);

this.setWaitMode('action');}

}

}

The code works when the last four lines aren't there.
It might help if you enclose everything you want to happen under the conditionals in curly braces
if(statement){
} else {
}
It might help if you enclose everything you want to happen under the conditionals in curly braces
if(statement){
} else {
}
Ok, just changed it so it's easier to read
It might help if you enclose everything you want to happen under the conditionals in curly braces
if(statement){
} else {
}
I just tried that on my actual code except I didn't put the braces around the else statement and it just worked??? so thank you I guess?
else ifs should be written like if (statement) { actions } else if (second condition) { alternate actions }​
​
Yours are written where in the else condition you have a whole other unrelated if statement.​
Ok, just changed it so it's easier to read
The curly braces are not just for readability. You're directing the logical flow of instructions to be executed under what conditions to the computer. Without them it's just going to execute everything follwing the statement and not know when to stop.
For this specific case, you'd be better off not using if statements at all, but a switch statement.

JavaScript:
switch ($gameVariables.value(11)){
    case 0:
        $gameMessage.add("Click");
        break;
    case 1:
        subject.forceAction(216, -2);
        BattleManager.forceAction(subject);
        this.setWaitMode('action');
        break;
    case 2:
        subject.forceAction(217, -2);
        BattleManager.forceAction(subject);
        this.setWaitMode('action');
        break;
    default:
        console.log('unexpected input')
}

This allows you to add to it easily, while keeping the code readable, and also has a 'default' case for if your variable has an incorrect value, it outputs to the console to warn you there's something wrong with it.

Original thread: https://forums.rpgmakerweb.com/threads/script-code-isnt-running-properly-when-adding-more-else-if-statements.174392/