Script call throws error message

The following script call is a troop event.  I have given it line numbers just to make it easy for me to refer to each bit.  Obviously as a script call, it doesn't normally have them.  This script call is on a page set to turn 1 and every subsequent turn, and the span is Turn.

a=0$game_party.members.each{|actor| #if actor.state?(77) b=(actor.hp*5/100) actor.hp-=b a+=b #end}$game_troop.members[1].hp += aWhat I think is happening with this script call is this.

Line 1 - establishes that a has a zero value at the beginning.

Line 2 - establishes that what follows is for all actors who are currently in the battle party.

Line 3 - checks whether each actor has state 77 inflicted on them

Line 4 - establishes the value of b.  In this case it is equal to 5% of every actor's current HP

Line 5 - Each actor has HP to the value of b subtracted from their current HP

Line 6 - a is now given the value of b

Line 9 - An enemy (in this case the enemy with the troop ID of 1) is given HP to the new value of a

This action is in addition to the skills in the enemy's skill list.

State 77 is inflicted on all the party by an event command at turn 0.  I have checked that this works by giving the state a distinctive icon and it appears at that moment.  Dialogue which follows the state being inflicted runs as expected.

I choose the party's actions and at the end of that the game crashes with the following error message.

'Game_Interpret' line 1411: NoMethodError occurred

undefined method 'hp' for nil:NilClass

I cannot see what is wrong with the script call to produce that error.  Could anyone help me out please?

Thank you.
Do you have two monsters on the enemy party?

On that last line of the script call, you're changing the HP of the second monster (index 1). It's the only thing I see that can cause a problem.
Enable console and put this above the script:

Code:
p "Actors"p $game_party.membersp "Enemies"p $game_troop.members
Yeah, like Hudell said, you probably need to change '$game_troop.members[1]' to '$game_troop.members[0]' if you only have one enemy.
That was it.  I keep forgetting that the index starts at 0.  When the last line was changed, everything went smoothly.

Thank you all so much.  I've been tearing my hair out over this.

Original thread: https://forums.rpgmakerweb.com/threads/script-call-throws-error-message.40968/