How to save an actor's ID to a variable when they use an item on themselves?

Okay, so I've tried several different variations of this javascript code and I just can't get this working properly. I've got an item that calls on a common event, which needs to know which character has used it. This item can be used both inside and outside of combat, and when used outside of combat it works perfectly fine. However, it doesn't want to work during combat. Can anyone help me here?

Code:
If: Script: $gameParty.inBattle()
  $gameVariables.setValue(7, $gameActors.actor(BattleManager._action._subjectActorId))
 
  Else:
  $gameVariables.setValue(7, $gameParty._targetActorId)

p.s. - I'm not trying to use plugins here.
If you're trying to save the actor's ID, why are you sticking it inside $gameActors.actor() to retrieve the entire actor object?

It should just be BattleManager._action._subjectActorId
If you're trying to save the actor's ID, why are you sticking it inside $gameActors.actor() to retrieve the entire actor object?

It should just be BattleManager._action._subjectActorId
that's because I already tried;
Code:
If: Script: $gameParty.inBattle()
  $gameVariables.setValue(7, BattleManager._action._subjectActorId)
 
  Else:
  $gameVariables.setValue(7, $gameParty._targetActorId)
and it didn't work. It doesn't return an error message or anything, but it doesn't actually store the ID of the actor to variable #7; variable #7 is still 0.

Edit: I've just discovered that this bit of code somehow breaks what I had, because it is returning a TypeError whenever I try to use the item on the map;
Cannot read property '_subjectActorId' of undefined
Edit 2: correction, both of these are returning this same error when I try to use the item on the map screen. I've only just noticed, and I can't remember which version of this code didn't do that.

Edit 3: I'm starting to think maybe the first line of code is the issue;
Code:
If: Script: $gameParty.inBattle()

that's because I already tried;
Code:
If: Script: $gameParty.inBattle()
  $gameVariables.setValue(7, BattleManager._action._subjectActorId)
 
  Else:
  $gameVariables.setValue(7, $gameParty._targetActorId)
and it didn't work. It doesn't return an error message or anything, but it doesn't actually store the ID of the actor to variable #7; variable #7 is still 0.

Edit: I've just discovered that this bit of code somehow breaks what I had, because it is returning a TypeError whenever I try to use the item on the map;

Edit 2: correction, both of these are returning this same error when I try to use the item on the map screen. I've only just noticed, and I can't remember which version of this code didn't do that.

Edit 3: I'm starting to think maybe the first line of code is the issue;
Code:
If: Script: $gameParty.inBattle()
Okay, so I've just tested a theory and used each line of code by itself. When it is just;
Code:
$gameVariables.setValue(7, BattleManager._action._subjectActorId)
it works as intended in battle. likewise, when it is just;
Code:
$gameVariables.setValue(7, $gameParty._targetActorId)
it works as intended on the map screen. This must mean that their is something wrong with the if/else part of the code.
it works as intended on the map screen. This must mean that their is something wrong with the if/else part of the code.
can you show a screenshot of what you have? because slapping $gameParty.inBattle() into the script box of a conditional branch absolutely works if you dont have any interference from a plugin
can you show a screenshot of what you have? because slapping $gameParty.inBattle() into the script box of a conditional branch absolutely works if you dont have any interference from a plugin

well, now I feel a bit dumb... I've just changed it and now it works. Thanks for the help y'all.
I've just changed it and now it works
yes.

when you use the script event command, the contents must be valid javascript. the syntax you used wasnt JS, it was how conditional branches were represented. either your correction, or
Code:
if ($gameParty.inBattle())
  $gameVariables.setValue(x,y)
else
  $gameVariables.setValue(x,y)
as the script would have worked, but trying to mix both and present it as script wouldnt

Original thread: https://forums.rpgmakerweb.com/threads/how-to-save-an-actors-id-to-a-variable-when-they-use-an-item-on-themselves.174200/