How to script a rarity system?

Hello devs! I am not much of a scripter so I was curious, how do I script a rarity system? I have searched far and wide and found nothing that is simple enough for what I need.

What I need is:

I am trying to make a game where you click on a button, and you have like a 50% chance of getting a “Common” badge, a 35% chance of getting an “Uncommon” badge, a 10% chance of getting a “Rare” badge, etc. There would be some really extensive badges like “Godlike” where the chance is something ridiculous like 0.00000000000001% chance.

Could someone tell me how to script this?

5 Likes

You should check out AlvinBlox’s YouTube Tutorial on how to make an egg hatching system because it teaches you how to hatch eggs with a chance of hatching a common, uncommon, rare, ultra-rare, and legendary pet.

You can also apply the same knowledge to your clicking game (or whatever it is) to get a badge with a 50% chance, a 35%, a 10%, etc.

Hope this helps!

3 Likes

All you need to know is badge service and math.random()
https://developer.roblox.com/en-us/api-reference/class/BadgeService

after that, create a chance system like

type or paste code here
```Button.MouseButton1Down:Connect(function()
      local Chance = math.random(1, ?) -- ur own number
      if Chance <= 5 then -- ur own number too

      elseif
               -- you will need to write it yourself --
         end
end)

-- Hope this helps! --
3 Likes

I solved this problem quite recently, this is what I did

local function chooseIndex(multipliersArray)
	local weightedSum = 0
	for i,v in pairs(multipliersArray) do
		weightedSum += v
	end
	local random = Random.new()
	local rnd = random:NextNumber(0,weightedSum)
	for i,v in pairs(multipliersArray) do
		if rnd < v then
			return i
		end
		rnd -= v
	end
end

So multipliersArray would just be a dictionary for your multipliers, for example:

local multipliersArray = {
Common = 0.35,
Uncommon = 0.1,
Godlike = 0.00000000001,
}
17 Likes