Switch check script leads to game crash?

I'm trying to run a simple script that changes a variable when a switch is turned on:

if $Game_Switches[103] == true $Game_Variables[21] = 3 end

However, this script consistently crashes the game with a "NoMethodError," saying "undefined method '[]' for nil:NilClass." What am I doing wrong?
NM got it wrong
You're capitalizing game variables that have no caps at all.
You're capitalizing game variables that have no caps at all.
The error occurs regardless of capitalization.
when and where are you using that script?
when and where are you using that script?
In its own window above Main.
That will never work, because the game variables only exist after "New Game", they do not work at that moment.
This is how it should look like, no matter if you think it doesn't matter. It just does.

Ruby:
if $game_switches[103]
  $game_variables[21] = 3
end

If it's still failing, then post the affected script right away to allow us to find the root cause of the error message.
This is how it should look like, no matter if you think it doesn't matter. It just does.

Ruby:
if $game_switches[103]
  $game_variables[21] = 3
end

If it's still failing, then post the affected script right away to allow us to find the root cause of the error message.
I'm not sure I understand what you mean. That's the entire script so far.
The one confused here gotta be me...
Are you telling us that you simply created a new script section in the editor and pasted that there?
Like really!? No wonder why it'd never work.

Let me explain you this. When the game is loading, aka the big black nothing screen, it won't ever find those variables because they're created or loaded after you have opened the Scene_Title or the Scene_Load scenes and quickly switch to the Scene_Map. So yeah, it SHOULD FAIL otherwise!
The one confused here gotta be me...
Are you telling us that you simply created a new script section in the editor and pasted that there?
Like really!? No wonder why it'd never work.

Let me explain you this. When the game is loading, aka the big black nothing screen, it won't ever find those variables because they're created or loaded after you have opened the Scene_Title or the Scene_Load scenes and quickly switch to the Scene_Map. So yeah, it SHOULD FAIL otherwise!
I see. I'm sorry, I'm still figuring out scripting and the reference guide is difficult for me to understand. I've made a lot of progress through trial and error and through reverse-engineering the code in the editor, but I'm still in the dark about a lot!

Maybe I should clarify what I'm actually trying to accomplish, and we can see if there's an easier way to go about it.

I'm trying to set it to where, while the player has an armor item equipped, a variable is increased by a set number. The variable would be decreased by that same number if the player unequipped the armor, so the variable bonus only exists while the armor is worn.

The easiest way I could figure to do that was:
1. Find a script that lets me tie switches to armor items.
2. Create a switch for each armor item in the game. While the armor is worn, its respective switch is turned on.
3. Set the game so that, if the switch was 'true,' the related variable is increased.
4. Set the game so that, if the switch was 'true' but the armor was not equipped, decrease the related variable and set the switch back to 'false.'

But alas, I'm stuck trying to get Step 2 to work.
But alas, I'm stuck trying to get Step 2 to work.
no, you're still at step 1, finding a way to tie switches to armor items.

because placing code into a script slot has nothing to do with the armor items, you need to place that code into the function that the game engine calls to equip an armor, and there it needs to check which armor and so on.
no, you're still at step 1, finding a way to tie switches to armor items.

because placing code into a script slot has nothing to do with the armor items, you need to place that code into the function that the game engine calls to equip an armor, and there it needs to check which armor and so on.
I've already found a script that does this.
Then you either need to enter the code for the variable change into that script, or use a parallel process in game to react to it (usually a bad idea unless you know exactly what you're doing) or create a framework script that places your code inside the game loop itself, but NOT place it into the general script structure outside the correct classes.
Then you either need to enter the code for the variable change into that script, or use a parallel process in game to react to it (usually a bad idea unless you know exactly what you're doing) or create a framework script that places your code inside the game loop itself, but NOT place it into the general script structure outside the correct classes.
Is that really the only way to check whether a switch is turned on?
First of all, what (custom) equipment script are you using? That should become our starting point to make sure we won't overwrite stuff you need somewhere else. It also includes any slight modification you've done to the default one if you haven't implemented any other script.
First of all, what (custom) equipment script are you using? That should become our starting point to make sure we won't overwrite stuff you need somewhere else. It also includes any slight modification you've done to the default one if you haven't implemented any other script.
Other than the one you made that allows for toggling switches with armor items, I'm not using any custom equipment scripts and I haven't messed with the default equipment script. That is, unless SDK does something I'm unaware of.
I made this add-on for EkuipSwitch XP, it's called EkuipVar XP.

UPDATED MY SCRIPT

Ruby:
# * EkuipVar XP -- EkuipSwitch XP Add-On * #
#   Scripter : Kyonides Arkanthes
#   2023-04-26

# This script works pretty much the same way EkuipSwitch XP script does.
# The only difference is that you must assign some unique Game Variable ID
# for every Equip Slot currently available in your game project.
# Setup the VARS_4_EQUIP_IDS Constant accordingly.
# 1st Slot stands for the weapon, the 2nd Slot for the 1st piece of armor, etc.

#   ACTOR_EQUIP_VARS = { ActorID1 => [Slot1Var, etc.], etc. }

# If you equip or unequip a weapon or armor included in one of the constants:
#   VAR_WEAPON_VALUES = { WeaponID1 => Value, etc. }
#   VAR_ARMOR_VALUES  = { ArmorID1 => Value, etc. }

module EKuipSwitch
  ACTOR_EQUIP_VARS = { 1 => [1, 2, 3, 4, 5] }
  VARS_4_EQUIP_IDS.default = [0, 0, 0, 0, 0]
  VAR_WEAPON_VALUES = {}
  VAR_ARMOR_VALUES = {}
  def self.var_value(type, item_id, equipped)
    return 0 unless equipped
    type == 0 ? VAR_WEAPON_VALUES[item_id] : VAR_ARMOR_VALUES[item_id]
  end
end

class Game_Actor
  def turn_equip_var(equip_type, equip_id, in_use)
    var = EKuipSwitch::ACTOR_EQUIP_VARS[@actor_id][type]
    $game_variables[var] = EKuipSwitch.var_value(equip_type, equip_id, in_use)
  end

  def update_equip_switches(equip_type, old_id, new_id)
    return if @is_temp_actor or old_id == new_id
    equip_type = 0 if equip_type == 1 and @dual_wield
    turn_equip_switch(equip_type, old_id, false)
    turn_equip_switch(equip_type, new_id, true)
    turn_equip_var(equip_type, old_id, false)
    turn_equip_var(equip_type, new_id, true)
    true
  end
end

Original thread: https://forums.rpgmakerweb.com/threads/switch-check-script-leads-to-game-crash.156955/