How do I make a Loot Table chance script?

Okay, so say, I have a monster that has the following weight loot table:

Cherries - 2
Gold - 1
Nothing - 2

I have a rough idea of how to do it in my head, but it just seems so complicated when I actually start writing. Basically, the sum of these weights are 5, and if you divide 100 by 5, you get 20, so if you multiply each weight by 20, you can obtain their chance percentages. So cherries and nothing are both 40 percent chance, leaving gold to have a 20 percent chance of dropping.

So how do I do it? I have no idea how to utilise math.random to do that

1 Like

like this?:

local Loot_Table = {
Cherries = 2,
Gold = 1,
Nothing = 2
}

local num = 0
for i, v in pairs(Loot_Table) do 
      num += v
end
num /= 100

for i,v in pairs(Loot_Table) do
     v *= num
end

Yes, I know how to do that part, but I’m trying to find out how to utilise math.random for this…you know, so it functions

Check the DevForum this has been answered many times!

Here’s one of them: