Battle Escape Item

I'm trying to make an item in my game that will, upon using it, will teleport your party back to a safe room, then remove all party members but the leader.
However, whenever I use the item in battle, it simply ends the battle and spits me back out in the map where I had the encounter.
Is there any way to have the item end the battle, then teleport me to another room and remove my party members? (The last part is totally optional, if that poses a problem.)
Probably? It's impossible to offer good advice on your event without seeing it.

If you post screenshots, we may be able to help.
Yes, you would just have to make the item call a common event and then put in the event commands for each of those.

E: Ok, you need to split it into two events because the map commands won't work until you're on the map. Try this:

JavaScript:
while($gameParty.members().length > 1){
    const actorId = $gameParty._actors.pop()
    $gameParty.removeActor(actorId)
}
sorry, I totally spaced out on adding the screenshot at first!
wilds.png

Here is the original script, but it looks like splitting it in half works great.
Thank you very much!

Original thread: https://forums.rpgmakerweb.com/threads/battle-escape-item.161005/