$gameTemp._tempEnemy error

Years ago I was trying to make a bestiary, and someone gave a code to create a temporary enemy, so I can get all his attributes and stats for my bestiary.
I don't remember who helped me with that. I decided to use it again, and a problem is happening just like it did years ago (I did not try to fix it). So even though I'm on a new project, the error is still there (the same one).

This is the script call:
Code:
var id = $gameVariables.value(41);
if (!!$gameTemp._tempEnemy) $gameTemp._tempEnemy.transform(id);
else $gameTemp._tempEnemy = new Game_Enemy(id, 0, 0);
The variable 41 is the enemy ID that I want to create so I can show his info at the bestiary.
If I use an item called Bestiary, It calls a common event that I can choose which enemy to see his information. Then it sends me to the common event that has this above script call, so I can get the attributes of the enemy and send it to the bestiary text.
If I'm in combat, I just use a Scan skill that calls the same common event.
So this script call is used inside and outside battle.

- After doing some fights, an error can occur (image 1) when I enter combat.
- I will also show the console (image 2).
If I reload the save right before the battle, the error does not occur again. So my theory is that a tempEnemy that was created on a previous fight is responsable for making the error happen on the current fight that crashed the game.

I don't understand much about the subject...
scripts depend on context, and you give none of that.

please give a screenshot of the common event, and especially one including where you set the variable 41.

because the error basically tells that the enemy you try to handle does not exist, which means the variable 41 contains the wrong number
As I said, I have to use the skill Scan inside battle, or the item Bestiary outside battle.
When I start the combat, the common event is not used. So maybe the tempEnemy still exists when I start the combat, and it causes issues.

- Image 1 receives the Enemy Troup, so it tells which enemies are inside a Enemy Troup.
It is used for the Scan skill. Turn 0 tells which Enemy Troup it is (Image 4).
- Image 2 receives the ID of the enemy that needs to get info. The tempEnemy is created.
Bestiary calls this common event directly, since it does not need the steps from Image 1.
- Image 3 writes the bestiary text of the enemy, using the info that was set from Image 2.

Since it is trying to create the tempEnemy, but it can't, then maybe I have to kill-destroy-erase the tempEnemy after checking the bestiary, right? Is there a way to do that?
Why do you give a screenshot of troop 5 if the events are about enemy 5?
can you show us a screenshot of the enemy 5 instead?
Why do you give a screenshot of troop 5 if the events are about enemy 5?
can you show us a screenshot of the enemy 5 instead?
The event is about ALL enemies. All enemies are in this common event. These events can receive the ID of any enemy in the game.
The error has happened even without ever fighting or doing anything with Enemy 5. Last error occured by fighting and using the bestiary of only enemies 12 and 13 (right now on my new testing).
Just like it has happened before without ever facing enemies 12 and 13.

And the enemy troup is where it receives the Enemy Troup ID, so it is related to the entire process of creating this bestiary.

Code:
<hit plus: +100%> Hit Chance
<fdr plus: -10%> Magic Hit Chance
<AOE Hitbox Height: 10>
<Scale Sprite: 70%>
<Default Counter: 703>

<AI Priority>
Random 20% +++ State === State 44 +++ Element Water Weakness: Skill 703, random
State === State 44 +++ Random 40%: Skill 703, random
Random 40% +++ Element Water Weakness: Skill 703, random
State !== State 44 +++ Random 40%: Skill 704, random
Random 80% +++ Element Water Weakness: Skill 703, random
Always: Skill 703, random
</AI Priority>

<Enemy Drops>
Essence of Life: 25%
Essence of Mana: 25%
Goop: 40%
Wet Goop: 20%
</Enemy Drops>

edit: added another image that shows another part of the common event. As I said, the tempEnemy is created for the enemy ID I choose, and this common event writes and receives the info of any enemy I want.

Original thread: https://forums.rpgmakerweb.com/threads/gametemp-_tempenemy-error.154532/