Rarity System [Need Help]

Thank you very much for the help!

1 Like

I saw the error, fixed it right away lol!

1 Like

Use math.random and make it check if its between these numbers like

local pet = math.random(100,1)
if pet =<50 and pet => 25 then 
-- give a pet that has a chance between these numbers
end
1 Like

May I ask how do people have pets display at like 0.00005%

Make it from 1 to 1000 so if you make it check if its 1 then it will be 0.1 chance

1 Like

There’s a tutorial for this in the Devforum, it is called “Weighted Chance” system.

3 Likes

Glad you fixed your issue.

May I recommend looking into weighted probability? That way, you have more control and fine-tuning over percentage proability rather than having to use a range* of values instead.

For example, the psuedocode could look like this:

local rarityModule = {}

local weights = 
{
  ["Common"] = 1,
  ["Uncommon"] = 0.5,
  ["Rare"] = 0.25,
  ["Epic"] = 0.125,
  -- etc., etc.
}

-- The ratio of Common to Rare is 1:0.25 = 4:1, which means for every 4 Commons you will on average receive one Rare
-- This allows for strong fine-tuning and knowledge of the odds without having to do unnecessary math

local function rarityModule.grabRarity()
  -- do stuff to return a rarity chosen by its weight (you can look this up, it has been implemented many ways in Lua)
end

return rarityModule

You could even do this to store a lot of information about a given rarity using the table:

local weightsWithExtraInformation = 
{
  ["Common"] = {weight = 1, value_multiplier = 1, damage_multiplier = 1},
  ["Uncommon"] = {weight = 0.5, value_multiplier = 2.5, damage_multiplier = 2},
  ["Rare"] = {weight = 0.25, value_multiplier = 4, damage_multiplier = 3},
  -- etc., etc.
}
3 Likes

Ok, so if say I did (1, 10000) would that make it 0.01%?

I have actually been trying to figure out the best way to do that, as this is somewhat new to me, recently got into the weighted functions and such!

1 Like

Thank you very much! Really appreciate the help.

1 Like

Unfortunately, Lua doesn’t have any native libraries that allow for weighted distributions. Someone else posted a link to a tutorial, try checking that.

The Alias method may also be worth reading into

1 Like

Yes it will do (10000/1 chance )

1 Like

Interesting, Thank you so much, I feel a bit like I have no brain cells as that seems to make sense. Idk why I never thought you could go higher than 100. I’ve had rarities as low as like 0.009% with just using the (1,100)

You are welcome have a nice day

1 Like

uuhhh i suggest doing this instead

local rarityChance = {}

rarityChance.Rarity = {
   Uncommon = 50,
   Common = 25,
   Rare = 15,
   Ultra = 5,
   God = 1,
}

return rarityChance
1 Like

Mark the solution if you found one.

1 Like

I used code from everyones help and mixed and matched. So technically I cant exactly mark which one.

1 Like

The post which mostly helped you.

Has already been posted above.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.