MV Uncaught TypeError: Cannot read property 'members' of null

Hi, i'm trying to make a plugin that shows the party learder's health in the screen. Well i'm trying to check if party leader's health is equals to 450 and when i try to check this with plugin i am getting this error:

error.png


And when i'm trying to do this with console, i'm not getting any errors. The code works perfectly, btw here is the code:

JavaScript:
if ($gameParty.members()[0]._hp == 450){
    console.log("450")
}

Why am i getting an error while i running this code with plugin? (Btw sorry for my bad english)
By the way if you are wondering the plugin code, here it is:
JavaScript:
(function() {
    if ($gameParty.members()[0]._hp == 450){
        console.log("450")
    }
})();
"of NULL" means that the object which has the function or data requested does not exist.

in this context, it most likely means that either the party or the members are not yet defined at the moment your code is executed.

the party and quite a number of other object like variables are only defined at "new game", not at the title screen or before that.
So most likely you created that function in a context where it is executed before the "new game" is started.
"of NULL" means that the object which has the function or data requested does not exist.

in this context, it most likely means that either the party or the members are not yet defined at the moment your code is executed.

the party and quite a number of other object like variables are only defined at "new game", not at the title screen or before that.
So most likely you created that function in a context where it is executed before the "new game" is started.
Thank you about that but, how am i going to fix this issue? Like, am i should check if player is not in the main menu or something? And how am i going to do that?
Hi there!

You need to know when you will execute this function.
Hi, i'm trying to make a plugin that shows the party learder's health in the screen.
So it seems kind of a Hud? So you want to show it on the scene map? Scene battle?

If so, you need to take a look at one of these scenes and understand the flow of it. After that, you will know when you will call your function.

You can also, just to be safe, made a check before using it:
JavaScript:
if($gameParty.exists()){
do your code...
}
Hi there!

You need to know when you will execute this function.

So it seems kind of a Hud? So you want to show it on the scene map? Scene battle?

If so, you need to take a look at one of these scenes and understand the flow of it. After that, you will know when you will call your function.

You can also, just to be safe, made a check before using it:
JavaScript:
if($gameParty.exists()){
do your code...
}
Yes i want to show an image when player is on scene map but i couldn't do it. Can you help me about this please? And of course i should check the player's health too. For an example:
JavaScript:
if (// scene map){
    if ($gameParty.members()[0]._hp == 450){
            // shows the health of the party leader with image
    }
}
Well, that's kinda complicated to say exactly what you have to do, but you have to take a look at the Scene Map class and see where you can create your HUD. So by the name of the functions you can have a hint of it. For example:

JavaScript:
Scene_Map.prototype.createDisplayObjects = function() {
    this.createSpriteset();
    this.createMapNameWindow();
    this.createWindowLayer();
    this.createAllWindows();
};

You can alias that function and create your hud before everything. Or you can use the AllWindows. But that is an example.
So you check: if game party exists && $gameParty.members()[0]._hp == 450...
do your code.

You can also use $gameParty.leader() that will always get the first member of the party($gameParty.members()[0])
Well, that's kinda complicated to say exactly what you have to do, but you have to take a look at the Scene Map class and see where you can create your HUD. So by the name of the functions you can have a hint of it. For example:

JavaScript:
Scene_Map.prototype.createDisplayObjects = function() {
    this.createSpriteset();
    this.createMapNameWindow();
    this.createWindowLayer();
    this.createAllWindows();
};

You can alias that function and create your hud before everything. Or you can use the AllWindows. But that is an example.
So you check: if game party exists && $gameParty.members()[0]._hp == 450...
do your code.

You can also use $gameParty.leader() that will always get the first member of the party($gameParty.members()[0])
Well yeah this is a little bit complicated i'll try this out thank you but, is there any way for just checking if the scene is scene map?
You don't need to check if the scene is "Scene Map" if you create your hud on the scene map.
That's why you first have to understand the RM code or understand it as you do your plugins.
In this case, you should take a look at the Scene Map class and see how they implement, for example, the Game Timer or the Map Name Window.

Then you follow the references until you understand how both appear on the map.

After that, you will know for sure how you can put your hud there ^^
You don't need to check if the scene is "Scene Map" if you create your hud on the scene map.
That's why you first have to understand the RM code or understand it as you do your plugins.
In this case, you should take a look at the Scene Map class and see how they implement, for example, the Game Timer or the Map Name Window.

Then you follow the references until you understand how both appear on the map.

After that, you will know for sure how you can put your hud there ^^
Thanks for the advice! 🙂

Original thread: https://forums.rpgmakerweb.com/threads/uncaught-typeerror-cannot-read-property-members-of-null.133780/