MV Short javascript question dealing with Actor Names

I have a bit of code here where to turn on a certain function I type for the Plugin Command

WeightSystemOn Actor
This lets me change the TP of the actor with other plugin commands. I then get the error which is TypeError: Cannot property 'IsActor' of null the line is in the else portion if (actor.isActor()) { which refers the line in the second code if (command === 'WeightSystemOn') this.switchWeightSystem(true, args); I believe I am getting the error because the Actors name has spacing its Celina Smokes any help would be great. Would I be able to set it to go by Actor ID instead of the name?

Code:
 Game_Interpreter.prototype.switchWeightSystem = function (flag, args) {
         var actorName = args[0];
        var actorId = 0;
        if (actorName === 'player')
            actorId = $gameParty.leader().actorId();
        else
            actorId = WeightManager.findActorIdByName(actorName);
        var actor = $gameActors.actor(actorId);
        if (actor.isActor()) {
            if (flag) actor.weightSystemOn();
            else actor.weightSystemOff();
        }
    }

Code:
var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
    Game_Interpreter.prototype.pluginCommand = function(command, args) {
        _Game_Interpreter_pluginCommand.call(this, command, args);
        if (command === 'SetWeight') this.setWeight(args);
        if (command === 'SetWeightType') this.setWeightType(args);
        if (command === 'WeightSystemOn') this.switchWeightSystem(true, args);
        if (command === 'WeightSystemOff') this.switchWeightSystem(false, args);
    };
Uh...what's the error message? Something like "cannot reference property weightSystemOff of undefined", I'm guessing? 😅

Note that you can use [code]code[/code] tags on these boards to format code nicely~
Spoiler: Original code + debug line
JavaScript:
Game_Interpreter.prototype.switchWeightSystem = function (flag, args) {
  var actorName = args[0];
  var actorId = 0;
  if (actorName === 'player') actorId = $gameParty.leader().actorId();
  else actorId = WeightManager.findActorIdByName(actorName);
  var actor = $gameActors.actor(actorId);
  console.log(actor);  // <- added for testing!
  if (actor.isActor()) {
    if (flag) actor.weightSystemOn();
    else actor.weightSystemOff();
  }
};
Here I've added a console output to help you check the value of your actor variable (F8 during playtest to open the console). Most of the code here looks OK so here's a couple of guesses:
  • The WeightManager.findActorIdByName function may be undefined, lacking a return statement, and/or not producing the expected output.
  • The switchWeightSystem method may be being given the wrong arguments.
I think I figured it out it was due to my Actor having a space in her name I changed it just to Celina and it works fine now.
you can use spaces in the name, but you need to tell the script
in case it contain spaces, it read it with (/\s*\more codes\s*) it depends on the code.
\s* = space or if it includes spaces, but I got someone that helped me with some
codes and this.

Original thread: https://forums.rpgmakerweb.com/threads/short-javascript-question-dealing-with-actor-names.127968/