MV "Cannot read property 'actorId'" error when Removing character in Battle

I'm getting this error "Cannot read property 'actorId' of undefined when removing a character during battle. It is a summoned character so when it dies, it automatically remove itself from the party. But whenever that happens, I get this error. Fortunately, I know the cause and it is the script below.
It is a script from here https://forums.rpgmakerweb.com/index.php?threads/yr-plugins.69148/ "BattlerChange" is the script name by YoraeRasante. I'll attach the files. If anyone can help fix the script, it would be very helpful. Thanks in advance.


var DB_GP_removeActor = Game_Party.prototype.removeActor;
Game_Party.prototype.removeActor = function(actorId) {
if (this.inBattle()) var i = this.battleMembers().indexOf($gameActors._data[actorId]);
DB_GP_removeActor.call(this, actorId);
if (this.inBattle()) {
SceneManager._scene.actorsToRefresh = SceneManager._scene.actorsToRefresh || [];
for (; i < this.battleMembers().length; i++) {
SceneManager._scene.actorsToRefresh.push(this.battleMembers().actorId());
}
}
"Of undefined" always means that the engine tries to access a data object that does not exist.

either the actorid is wrong, or some part of the function is called after the actor is already removed or you have an indexing error or something like that.

EDIT: and in case of yanfly's plugins, I would check if they are in the correct order - they have to be in exactly the order given on his website, and deviation from that can cause errors.
The proper way to fix this would be to investigate why Game_Party.battleMembers() contains undefined objects. At a quick glance, that should not be the case. But since that can have any number of reasons which may be entirely unrelated to the plugin that actually throws the error this would involve a lot of work.
Though I will recommend looking at the way you remove the summoned actor from the party. Maybe that is the cause?

In any case the quick and dirty way to 'fix' this would be to check whether the battle member actually exists before accessing its actorId:
JavaScript:
//Code from the plugin you posted
//...
for (; i < this.battleMembers().length; i++) {
    //insert this line
    if(this.battleMembers()[i])
        SceneManager._scene.actorsToRefresh.push(this.battleMembers()[i].actorId());
}
//...

Just be aware that this is applying a plaster to this specific symptom and does nothing to address the underling issue. Any other time some code tries to access data from Game_Party.battleMembers() this error will still show up.
The proper way to fix this would be to investigate why Game_Party.battleMembers() contains undefined objects. At a quick glance, that should not be the case. But since that can have any number of reasons which may be entirely unrelated to the plugin that actually throws the error this would involve a lot of work.
Though I will recommend looking at the way you remove the summoned actor from the party. Maybe that is the cause?

In any case the quick and dirty way to 'fix' this would be to check whether the battle member actually exists before accessing its actorId:
JavaScript:
//Code from the plugin you posted
//...
for (; i < this.battleMembers().length; i++) {
    //insert this line
    if(this.battleMembers()[i])
        SceneManager._scene.actorsToRefresh.push(this.battleMembers()[i].actorId());
}
//...

Just be aware that this is applying a plaster to this specific symptom and does nothing to address the underling issue. Any other time some code tries to access data from Game_Party.battleMembers() this error will still show up.

Just tested the script multiple times, it worked like magic! Thank you!
it worked like magic! Thank you!
you have read that what he gave you is not a bugfix, but is only hiding the still existing bug?
You will get other errors as long as there are undefined objects on the battlemembers, and those undefined objects are most probably caused by something seriously wrong with the summoning...
you have read that what he gave you is not a bugfix, but is only hiding the still existing bug?
You will get other errors as long as there are undefined objects on the battlemembers, and those undefined objects are most probably caused by something seriously wrong with the summoning...

I think the order with Yanfly scripts should be correct. I make sure I follow the videos everytime. Maybe it's the other scripts, but I know I started getting this error when I added the dragonbone script I shown above. It is not the summoning. I get this bug when removing actor from party in mid battle. I think it's the troops event setting.
I'm trying to effectively remove summoned actors when they die. Here is how I set them.

Original thread: https://forums.rpgmakerweb.com/threads/cannot-read-property-actorid-error-when-removing-character-in-battle.143803/