How to make a weighted rarity system!

Introduction

Hello there! My name is Icy. an advanced programmer, builder, and web developer who has been on the roblox platform since mid 2018! I started roblox when I turned 11 in 2018, a friend had suggested to me. Ever since, I’ve been playing and developing games!

The Tutorial

today I will be showing you how an Weighted Rarity System, Basically the basics.

Step 1

before we begin scripting our rarities we must choose our rarities! Type the following to begin.

local RarityTable = {
	Common = 45;
	
	Rare = 35;
	
	Epic = 18;
	
	Legendary = 2; -- these rarities are changeable. Change the numbers how ever you want them! 
}

Step 2

Now we can start create our system! Type in the following on lines 11 - 15.

local weight = 0

for _, Chance in pairs(RarityTable) do
	weight += (Chance)
end

What this will do is create the start of our rarity table by adding the Rarity Value with the weight.

Step 3

Next we can create our Printing and final rarity functions. Type in the following from lines 17 - 27.

local ranNum = math.random(1, weight)

weight = 0
for Rarity, Chance in pairs(RarityTable) do
	weight += (Chance)
	
	if weight >= ranNum then
		print("Rarity "..Rarity.." has been discovered!")
		break
	end
end

Basically the same thing as step 2. What this will do is create and print the rarity name and value!
https://gyazo.com/e1c3a0c0778ca1963627e99a8730c5fb

Conclusion

You can use this rarity system for crates, eggs and more! feel free to change what ever you would like! If you have any other ways of improving this system, Please let me know!

15 Likes

Let’s say we want to implement a weighted rarity system with an outside factor: Luck. How would we achieve this? Let’s say someone buys an egg and that person has double luck on them. How would that be applied?

I think it would basically double the random number, thats an easy way to do it.

1 Like

In my opinion it would’ve been better to also supply the full script not in steps, could you give me that?

What I do for this is just make the rarity values attributes that can be modified.