Galv's random loot script isn't working and I don't understand what I did wrong.

Hello everyone,

what the title says basically.

I did everything according to the instructions but nothing is happening.

This is the script:

Ruby:
#===============================================================================
#                          +++ GALV'S RANDOM LOOT +++
#===============================================================================
# + Original Script by Galv
# + Extended by Fhizban (also known as Malagar)
# + Version 1.4
# + Updated August 2015
# + For RPGMaker VX Ace
# + Free for Non-Commercial and Commercial use
# + Requires no other scripts
#===============================================================================
#                                DESCRIPTION
#===============================================================================
#  This script allows you to have a script call to gain random loot from chests.
#  Add notetags to items to specify level requirements and rarity values.
#  A notetag can also be added to items that you want to increase the chance to
#  find rarer loot. Random loot can be limited to certain types of items too.
#===============================================================================
#                                INSTALLATION
#===============================================================================
# + Put this script after Material but before main.
# + Add some of the notetags (described below) to the items in the database.
# + Add a new event that represents a chest or other container to your map.
# + Add a script call to the event that will generate random loot (see below).
#===============================================================================
#                                DOCUMENTATION
#===============================================================================
# You are required to setup two different parts to make this script work:
#   1) Add Notetags to your items in the database.
#   2) Add the script call to your events (chests).
#   3) Optionally you can change the configuration below to your taste.
#
# 1) Editing Notetags:
#    You must specify the following notetags for all ITEMS, WEAPONS and ARMOR that
#    you want to randomly appear in your events (chests). If any items do not have
#    these tags, they will just not appear in a random chest at all.
#
#    <family: x>                    # The group or family the item belongs to
#                                   # You define these families yourself (1-99)
#    <rarity: x>                    # The rarity value (1+ higher is rarer)
#    <level-min: x>                 # Min level of player for item to show (1-99)
#    <level-max: x>                 # Max level of player for item to show (1-99)
#
#    Optional notetag for ARMORS and WEAPONS only:
#
#    <lucky: x>                     # All equipped lucky items of the party are added to
#                                   # calculate the total chance to get rarer items.
#
# 2) Using the script call:
#    By using this script call, you can randomly obtain an item. Go to your event and add
#    a new command to it, choose script from the very last command tab and insert the
#    following line.
# 
#    EXAMPLE:
#
#            random_item(0, 1, 10, 50, 1, 1)
#
#    SYNTAX:
#
#            random_item(type, rarity_min, rarity_max, subtype=0, monster_id=0, family=0)
#
#    type                           # The type of item to be found:
#                                   # 0 = Random/any type (you can also use numbers 4+)
#                                   # 1 = Item
#                                   # 2 = Armor
#                                   # 3 = Weapon
#
#    rarity_min                     # Min and Max Rareness determine the item rarity that
#    rarity_max                     # the script call will obtain when successful. You can
#                                   # state any number that you also use on the notetags
#                                   # of your items. Example:
#                                   # rarity_min=10, rarity_max=50
#                                   # The script call will generate a random number 1-50.
#                                   # An item can only be obtained if it has a rarity
#                                   # EQUAL or LESS than the generated number.
#
#    subtype                        # OPTIONAL. This only applies to WEAPONS or ARMOR and
#                                   # will be ignored otherwise. Filters the random loot
#                                   # to drop only items of the stated type.
#                                   # (e.g. 1 = General Armour)
#
#    monster_id                     # OPTIONAL. This ID is used to decide what TROPP id
#                                   # will be used when a monster-in-a-box is encountered.
#                                   # Otherwise the standard troop will be used. Ignored
#                                   # if encounter chance is set to zero.
#
#    family                         # OPTIONAL. Family can be any number or 0.
#                                   # 0 = drops any item of the stated type/subtype
#                                   # x = drops only items of the stated type/subtype that
#                                   # share the family.
#
# Using a clever combination of type, subtype and families - you can tailor the random
# loot spawn to create only items of a very specific type. Like only Armors of the Small
# Shield type, or just Weapons of the Sword type and so on.
#===============================================================================
 
$imported = {} if $imported.nil?
$imported["Random_Loot"] = true
 
module Random_Loot
#===============================================================================
#  CONFIGURATION
#===============================================================================
  GET_MESSAGE = "Found "                    # Text before item name.
  GET_MESSAGE_AFTER = "!"                   # Text after item name.
 
  FAIL_MESSAGE = "It's empty!"              # Text when random item comes up nothing.
  
  SOUND_EFFECT = ["Coin", 90, 100]          # Sound effect of gaining an item.
                                            # ["SE Name", volume, pitch]
                                            # Make "SE Name" = "" for no sound.
                                    
  DEFAULT_MONSTER = 1                       # Default Monster ID if none stated.
 
  MONSTER_CHANCE = 100                       # % Chance that if no item is found, a
                                            # battle will happen instead.
  ESCAPE_MONSTER = true                     # Can you escape it? true or false
  
  MONSTER_MESSAGE = "Monster in a box!"     # Message when a monster appears.
                                            # Make it "" to disable.
#===============================================================================
#  END CONFIGURATION
#===============================================================================
end
 
#===============================================================================
module Random_Loot_Notetags
  def loot_rarity
    if @loot_rarity.nil?
      if @note =~ /<rarity: (.*)>/i
        @loot_rarity = $1.to_i
      else
        @loot_rarity = 99
      end
    end
    @loot_rarity
  end
  def loot_level_min
    if @loot_level_min.nil?
      if @note =~ /<level-min: (.*)>/i
        @loot_level_min = $1.to_i
      else
        @loot_level_min = 1
      end
    end
    @loot_level_min
  end
  def loot_level_max
    if @loot_level_max.nil?
      if @note =~ /<level-max: (.*)>/i
        @loot_level_max = $1.to_i
      else
        @loot_level_max = 99
      end
    end
    @loot_level_max
  end
  def loot_lucky
    if @loot_lucky.nil?
      if @note =~ /<lucky: (.*)>/i
        @loot_lucky = $1.to_i
      else
        @loot_lucky = 99
      end
    end
    @loot_lucky
  end
  def loot_family
    if @loot_family.nil?
      if @note =~ /<family: (.*)>/i
        @loot_family = $1.to_i
      else
        @loot_family = 99
      end
    end
    @loot_family
  end
  end # Random_Loot_Notetags
#===============================================================================
 
class RPG::Item
  include Random_Loot_Notetags
end
class RPG::Armor
  include Random_Loot_Notetags
end
class RPG::Weapon
  include Random_Loot_Notetags
end
 
#===============================================================================
class Game_Interpreter
 
  def random_item(type, rarity_min, rarity_max, subtype=0, monster_id=0, family=0)
  
    #--- Determine Type
    if type == 1
      @loot = $data_items
      subtype = 0
    elsif type == 2
      @loot = $data_armors
    elsif type == 3
      @loot = $data_weapons
    else
      @loot = $data_items | $data_armors | $data_weapons
      subtype = 0
    end
 
    #--- Preperations
    @loot.shuffle
    if rarity_max < rarity_min
      rarity_max = rarity_min
    end
    if subtype < 0 || subtype > 99
      subtype = 0
    end
    if family < 0 || family > 99
      subtype = 0
    end
    if monster_id < 1
      monster_id = Random_Loot::DEFAULT_MONSTER
    end
    
    #--- Determine Party Luck Bonus
    mem = 0
    eqs = 0
    luck_bonus = 0
    i = 0
    no_equips = $game_party.members.count * $game_party.members[0].equips.count
    $game_party.members.each do |mem|
      mem.equips.each do |eq|
        next if eq.nil?
        luck_bonus += eq.loot_lucky
      end
    end
  
    #--- Determine Rarity Chance
    checked = 0
    restart = false
    continue = true
    no_items = @loot.count - 1
    rare_chance = rand(rarity_max - rarity_min) + rarity_min + luck_bonus + 1
    random_item = rand(no_items) + 1
    begin_count = rand(no_items) + 1
  
    #--- Determine Random Item
    no_items.times do |i|
      i = i + random_item
      begin_count += 1 if restart
      if i > no_items
        i = i - i + rand(no_items) + 1
        restart = true
      end
    
      #--- Get Random Item
      if family != 0 && @loot[i].loot_family != family
        continue = false
      end
      if subtype != 0 && type == 2
        if @loot[i].atype_id == subtype
          continue = false
        end
      end
      if subtype != 0 && type == 3
        if @loot[i].wtype_id == subtype
          continue = false
        end
      end
 
      if continue
        if @loot[i].loot_level_max >= $game_party.leader.level && @loot[i].loot_level_min <= $game_party.leader.level
          if @loot[i].loot_rarity <= rare_chance
            if rand(1) <= 0 #--- Dirty Hack to skip some items
              $game_party.gain_item(@loot[i], 1)
              RPG::SE.new(Random_Loot::SOUND_EFFECT[0], Random_Loot::SOUND_EFFECT[1], Random_Loot::SOUND_EFFECT[2]).play
              $game_message.add(Random_Loot::GET_MESSAGE + "\\I[" + @loot[i].icon_index.to_s + "]" + @loot[i].name.to_s + Random_Loot::GET_MESSAGE_AFTER)
              wait_for_message
              return
            end
          end
        end
      end
      checked += 1
    
      #--- Trigger Monster-in-a-Box Battle
      if checked == no_items
        monster_chance = rand(100) + 1
        if monster_chance <= Random_Loot::MONSTER_CHANCE
          if Random_Loot::MONSTER_MESSAGE != ""
            $game_message.add("\\>" + Random_Loot::MONSTER_MESSAGE + "\\.\\.\\.\\^")
            wait_for_message
          end
          BattleManager.setup(monster_id, Random_Loot::ESCAPE_MONSTER, false)
          SceneManager.call(Scene_Battle)
        else
          $game_message.add(Random_Loot::FAIL_MESSAGE)
          wait_for_message
        end
      end
    end
  end
  
end # Game_Interpreter
 
#===============================================================================
# END OF SCRIPT
#===============================================================================

I think there is a mistake with the instructions, it says:

random_item(0, 1, 10, 50, 1, 1)
#
# SYNTAX:
#
# random_item(type, rarity_min, rarity_max, subtype=0, monster_id=0, family=0)

But the order is not matching? rarity_min comes second yet it's the 3rd in the script call (the number 10) so what is that 1 in second place? the subtype?

Also a screenshot of my chests (none are working just empty, not even a battle is happening with 100% to trigger one):

1746559459961.png
1746559488491.png


I made 2 versions because I don't understand how this works.

And some item tags:

1746559545137.png
1746559572341.png


Any help? Thanks in advance!
The script should be correct, except you have your script commands with quotations.
"random_item(1, 1, 9, 1, 1)"
remove them
random_item(1, 1, 9, 1, 1)
But the order is not matching? rarity_min comes second yet it's the 3rd in the script call (the number 10) so what is that 1 in second place? the subtype?
You are right in that it doesn't match the interpreter command.
The leading 0 can probably be removed. At some point it was added but not explained why.
I have 1.0 of the script, 1.4 that you have, 1.5 and 1.4+var
Only the 1.0 version does not have the leading 0 in the script command.
The script should be correct, except you have your script commands with quotations.
"random_item(1, 1, 9, 1, 1)"
remove them
random_item(1, 1, 9, 1, 1)

You are right in that it doesn't match the interpreter command.
The leading 0 can probably be removed. At some point it was added but not explained why.
I have 1.0 of the script, 1.4 that you have, 1.5 and 1.4+var
Only the 1.0 version does not have the leading 0 in the script command.
Hello, sorry for the late reply but thank you it works now.

But something else is problematic now, the random loot are items I don't have tags in the notes section yet they appear, do I need to change something?

EDIT: Nevermind it is working! I had changed some numbers in the script from 0 to 99 I wonder why I did that since I have no idea how scripting works 😅

It gives me the correct items now. I just need to make sure I can get the battle to trigger if it's empty. But for now thank you very much!

Original thread: https://forums.rpgmakerweb.com/threads/galvs-random-loot-script-isnt-working-and-i-dont-understand-what-i-did-wrong.177465/