Random object by rarity from number value inside of that object?

Hello!

What I’m trying to get?:
I’m trying to make a function getrandomobject() but with rarity number value that is inside of that object.
I want that rarity number to be any number. And i aslo don’t want it to choose by for example: Common, rare, extra rare. But i want to get it by number, Like higher number is, there is more chance to that function to choose it.

Then you can create a global loot table inside a ModuleScript or Script which has all the rarity number that you want it to drop. You also have to create a number which is a base number that the rarity number will be ranged. This is a starter table

local GlobalLootTable = {
       ["item1Rarity"] = {
              ["BaseRange"] = 10000, -- This number will be used as a range for your math.random() later.
              ["ChanceTable"] = {
                     ["ItemA"] = 1000, -- 10%
                     ["ItemB"] = 500, -- 5%
                     ["ItemC"] = 1, -- 0.1%
              }
       }
}

local function getRandomObject()
          for ItemName, Chance in pairs(GlobalLootTable["item1Rarity"]["ChanceTable"]) do
                  local randomNumber = math.random(GlobalLootTable["item1Rarity"])    -- This will allow multiple drops, but each iteration will get its random chance.
                  if Change <= randomNumber then
                        print("Drop!" .. ItemName)
                  do
          end
end

@Jacky2804 , Good idea, but i want it to get rarity number from numbervalue inside of that object. :grinning:

Do you have any idea about that?

Then it is even more easier, if you have objects in one folder, you can use GetChildren to retrieve all the rarity value inside NumberValue

for _ , item in pairs(folder:GetChildren()) do
      local rarity = item:FindFirstChild(“rarityValue”)
      print(“Rarity!” .. rarity.Value)
end

@Jacky2804 . I mean that this is my folder:
Zrzut ekranu 2023-03-06 175330
and the weight value is rarity number. there are rarity number scale from 0-2000 the higher rarity number is chigher chance to choose it from serverscript.

@Jacky2804 Do you have any idea?

Sorry, I didn’t see your reply yesterday.

So you can use a Script inside ServerScriptService to loop through that folder to get the value like this


local sectionsFolder = game:GetService("ServerStorage"):FindFirstChild("sections")

for _, child in pairs(sectionsFolder:GetChildren()) do
      local childRarity = child:FindFirstChild("weight")
      if childRarity == nil then    --Guarding
          continue
      end
      print("Rarity!" ,childRarity.Value)
end

@Jacky2804 Ok, it will get all objects rarities, but how can i choose random one by that data from that script?