What's wrong with this bit of code?

So I was working on the shop steal script, adding a switch based event.
The consequences worked perfectly without the if statements. I have been working on this code for a while now, and adding else doesn't work.
The problem is that the 2nd if statement is activates regardless weather or not switch 6 is on.
Does anyone know how to fix this?

Code:
  def do_steal(number)
    if $game_switches[6] = false
      life_or_death = rand(100)
      if life_or_death <= 50
        $game_party.gain_item(@item, 1)
      else
        $game_switches[21] = true
        SceneManager.goto(Scene_Map)
      end
    end
   
    if $game_switches[6] = true
      $game_switches[22] = true
      SceneManager.goto(Scene_Map)
    end
  end
Because you've assigned the switch value instead of checking whether it's true or false.
The symbol for EQUAL TO is "==", so change your if statement to something like this:
Code:
if $game_switches[6] == false
Because you've assigned the switch value instead of checking whether it's true or false.
The symbol for EQUAL TO is "==", so change your if statement to something like this:
Code:
if $game_switches[6] == false

Thanks! Consequences are now working perfectly now! It was just a common and tiny mistake.
To think of it, I wonder why the editor doesn't point out errors like this.
It assumes that we are not dumb. 😎
It assumes that we are not dumb. 😎
Even the smart make mistakes that they cant figure out.
The editor doesn't point out the error because it's not an error - at least not a syntax error. You used the assignment operator, which is a valid operator, even in an if statement.

if $game_switches[6] = false will always return true, because the value of false was successfully assigned to $game_switches[6].

The editor assumed you were trying to assign a value, and check if it was successful or not. It's not a mind-reader - it doesn't know you were trying to check the value. It has to go on what you tell it, and you told it to assign, not compare.

Because $game_switches elements will always be true or false, you don't even need to do the == true or == false. You can just do this:
if $game_switches[6]
which will return true or false, whatever the switch is set to.

So if you want to do something if the switch is on, do this:
if $game_switches[6]

and if you want to do something if the switch is off, do this:
if !$ game_switches[6]

! is the "not" operator, and returns the opposite of whatever is in the switch.


Even the smart make mistakes that they cant figure out.
And that is why there is a forum 🙂
Even the smart use stackoverflow.com lol

Original thread: https://forums.rpgmakerweb.com/threads/whats-wrong-with-this-bit-of-code.104827/