How to make a shop close after the player buys an item?

Hey all! So I'm working on a certain post game raid challenge for my project. The player will be able to enter a gauntlet of boss refights from the main story all scaled up to post game standards. The player will be able to choose various difficulties and after they clear all the fights, they'll gain access to a special shop with multiple tiers of rare items. Depending on what level of difficulty the player cleared, they'll have a certain number of credits to buy things from the various tiers(which also cost large chunks of standard gold). There will be tier 1: rare/unbuyable items for 1 credit, tier 2: unique boss drop weapons and armor for 2 credits, and tier 3: ultimate items/armor(only available in this shop) for 3 credits. I have it all layed out with variables and loops for the shops and tier choices. All I need is a way to force the shop menu to close after the player buys something so the event can continue; taking the credits away, seeing if the player has any left, and going back to the start of the shop loop if they do. Does anyone have a way to make this happen? Any kind of parallel process or script call method that would make this possible? Thanks!
I'm guessing that would be conditional branches with maybe self switches? Or a script might do it. Not entirely sure, but maybe one of my suggestions will help you
Not sure, but try return_scene. That will cause it to exit the current scene.
Not sure, but try return_scene. That will cause it to exit the current scene.
That's a good idea and I'd like to try that. Any idea how I could get it to detect if the player buys something?

I'm guessing that would be conditional branches with maybe self switches? Or a script might do it. Not entirely sure, but maybe one of my suggestions will help you
I've tried poking around with some of that, and the main issue is conditional branches can only detect if the player has an armor/weapon/item. If they want another of an accessory for instance, that wouldn't work. I'm trying to figure out how I can make the player's action of purchasing something during the shop trigger an event or script to make it close.

I made a test event for this shop in my debug world. It's all there except a way to detect when the player makes a purchase during shop processing and a way to make the shop close. Here's a screenshot for anyone curious:
1596346161701.png

Edit: the conditional branch to break the loop is copied at both the top and bottom of the choices, just realized that was cut off.
You can set a variable to the number of that item in inventory before doing the shop processing, and then set a different variable to the number of that item in inventory after the shop processing. If the newer number is higher, they purchased.
You can set a variable to the number of that item in inventory before doing the shop processing, and then set a different variable to the number of that item in inventory after the shop processing. If the newer number is higher, they purchased.
That's really good, I'll look into it and probably use something akin to that if nothing else works. The only roadblock I can think of is the player can make multiple purchases before exiting shop processing. I'm trying to think of a way to force it to close after the first purchase, no matter what it is.
Err, what about storing credits in a variable and just checking if that variable is not the same as your current amount of credits?

Then use "exit event processing" command.
If you want it to close after the first purchase, you'll need a script.
Err, what about storing credits in a variable and just checking if that variable is not the same as your current amount of credits?

Then use "exit event processing" command.
That's great, but the credits won't be taken away until after the player exits the shop. Or I'd need an event/script to take the credit(s) away on purchase as well as the gold.

Edit: Wait a minute, that might be it! I'm going to see if I can store gold in a variable and detect the decrease with an event.
If you want it to close after the first purchase, you'll need a script.
Yeah, that's what I need help figuring out. I tried return_scene like someone else said and it didn't recognize the less gold until after the shop closed(an idea i was testing for) and when it did notice, return_scene just caused an error about an undefined variable. I'd really like to make this work, I just need help with this one hurdle. It's so frustratingly close but so far...
Here's a little snippet that should help:

Ruby:
class Scene_Shop < Scene_MenuBase
  # if switch is set to TRUE, the shop will close after purchase
  SHOP_CLOSE_SWITCH = 2 # ID of the switch
 
  # variable for storing the last item purchased
  ITEM_ID_VARIABLE = 4 # ID of the variable
 
  alias amn_closeshop_sceneshop_dobuy do_buy
  def do_buy(number)
    amn_closeshop_sceneshop_dobuy(number)
    if ITEM_ID_VARIABLE > 0
      type = case @item
      when RPG::Weapon then "W"
      when RPG::Armor then "A"
      else "I"
      end
      $game_variables[ITEM_ID_VARIABLE] = "#{type}#{@item.id}"
    end
    return_scene if SHOP_CLOSE_SWITCH > 0 && $game_switches[SHOP_CLOSE_SWITCH]
  end
 
end

Paste that in its own slot below Materials and above Main.

Set the switch to TRUE before opening the shop. If you purchase an item, the shop window will close directly after, so long as the switch is TRUE. The last item purchased is stored in the ITEM_ID_VARIABLE like so: "I1" would be item 1 (e.g. potion in the default), "A1" would be armour 1 and "W1" would be weapon one. Then you can set up a conditional branch after the shop scene that checks if the variable is equal to "A1", etc.
It needs an update to the scripts, not just a script call in an event. Events don't run while menus (including shop) are open.
I recall how I managed to let the shop event stay open after the shop menu was closed by editing a specific method in the Interpreter class in XP. In other versions it'd be Game_Interpreter. This way it wouldn't stop processing the event allowing you to even set a custom goodbye message afterwards.

I've moved this thread to Script Requests. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/how-to-make-a-shop-close-after-the-player-buys-an-item.124762/