MV Need help storing weapon in variable then re-equip later

I'm making a common event that lets player swap between two weapons. How I'm doing it right now is I have another common event that lets player pick a weapon out of the weapons they have to be their second weapon, and I store the ID of the weapon in a variable. Then I use a change equipment script call.

But I'm having trouble storing the first weapon that's equipped via the actual equipment menu in a variable. If I use the change variable via script command and use this script "$gameActors.actor(4).weapons()". It will store the entire weapon data into the variable instead of just a number. And if I try to change equipment with the variable it will unequip the player's weapon instead, it doesn't give me an error message either.

Is there a script to just store the weapon ID of the currently equipped weapon?
Place this in the Variable's Script Box:
JavaScript:
var w = $gameActors.actor(4).weapons()[0]; w ? w.id : 0

Use this Script Call to re-equip the stored Weapon ID:
JavaScript:
$gameActors.actor(4).changeEquip(0, $dataWeapons[$gameVariables.value(1)]);
Place this in the Variable's Script Box:
JavaScript:
var w = $gameActors.actor(4).weapons()[0]; w ? w.id : 0

Use this Script Call to re-equip the stored Weapon ID:
JavaScript:
$gameActors.actor(4).changeEquip(0, $dataWeapons[$gameVariables.value(1)]);
This worked, thank you so much!

Original thread: https://forums.rpgmakerweb.com/threads/need-help-storing-weapon-in-variable-then-re-equip-later.179013/