Some Support needed on a 'chances' system

I am working on a pet system from scratch and I want my game to have a UI that pops up when you come close to an ‘incubator’ that shows the chances of getting a certain pet. (Like in Bubble Gum Simulator, out of 100%) But I can’t figure out how I would count my chances in my system. I have a ModuleScript Inside each egg(at the moment only one egg) in the workspace- the module has tables of each pet and its rarity also it has the function to choose a random pet. The script that I am having trouble with is in the StarterGui Inside the Frame that pops up when you come close to it.
Heres what I got

local number = 0
local Data = require(workspace.Eggs.BasicEgg.PetModule)
while true do wait() script.Parent.Visible = false
	for i,v in pairs(workspace:WaitForChild("PetsInfo"):GetChildren()) do
		if (v.Position-game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart").Position).Magnitude <= 8 then
			local ReplicatedStorage = game:GetService("ReplicatedStorage")
			local Pets = Data.pets
			local WorldScreenPoint = workspace.CurrentCamera:WorldToScreenPoint(v.Position)
			script.Parent.Visible = true
			script.Parent.Main.Position = UDim2.new(-0.1,WorldScreenPoint.X,-0.3,WorldScreenPoint.Y)
				for rarity, name in pairs(Data.pets) do --THIS IS THE PART I DONT UNDERSTAND AND I CANT GET IT RIGHT(if anyone needs I can post the script inside the module)
				number = number + 1	
				local clone = script.Cell:Clone()
				local Back = script.Parent.Main.Back
				clone.Parent = Back
				clone.Name = "Cell"..number
				for rarity, weight in pairs(Data.rarities) do
				local new = Back:FindFirstChild("Cell" .. number)
				local count = #Data.pets.Common + #Data.pets.Uncommon + #Data.pets.Rare
				new.Percent.Text = weight/count
				end
			end
		end
	end		
end

Thank you
Edit:
Here is the Module that’ll be in every ‘incubator’

local petModule = {}
petModule.pets = {
--pets in the egg
["Common"] = {
	game.ReplicatedStorage.Pets.Kitten;
	game.ReplicatedStorage.Pets.Doggo;
	game.ReplicatedStorage.Pets.Mouse;
};
["Uncommon"] = {
	game.ReplicatedStorage.Pets.Panda;
};
["Rare"] = {
	game.ReplicatedStorage.Pets.Pigeon;
};
}
--rarities in the egg
petModule.rarities = {

["Common"] = 75;

["Uncommon"] = 24;

["Rare"] = 1;

}
--choose a random pet function
petModule.choosepet= function()
	local randomNum= math.random(1,100)--out of 100
	local number = 0
	for i, v in pairs(petModule.rarities) do
		number = number+ v
		if randomNum <= number then
			local RARITYTable = petModule.pets[i]
			local chosenOne = RARITYTable[math.random(1,#RARITYTable)]
			
			return chosenOne
		end
	end
end
	
return petModule
3 Likes

I would just do this

local randomnum = math.random(1, 100)
if randomnum == 1 then
    --give rare pet
else if randomnum <= 25 then
   --give uncommon
else
   --give common
end

be sure to set the seed with math.randomseed(os.time())

2 Likes

math.random() is outdated, you should be using Random.new()

local PRNG = Random.new(seed)

PRNG:NextInteger(min, max)
PRNG:NextNumber(min, max)
1 Like

It is not outdated, the former now uses the latter under the hood. Using the former is just fine.

4 Likes

Good to know, thanks for the info.

Oh, hm didn’t think about that. Also by any chance would you know if theres a way I could use the randomnum to calculate the chances to getting a certain pet; There would be a 25% chance of getting a common pet

No no, its all good, ANYTHING would help, I spent the longest time thinking about this and I can’t get it so Thank You

I just uploaded a copy of my MathUtils module that I used on a project. In it you’ll find instructions on how to use the WeightedLottery function. Take a look:

1 Like

Of course, i’ll try it out, my studio is lagging at the moment but I’ll make sure to tell you if it works Thank you so much!

1 Like

So I have read through the the module but I’m not quite sure how to use it, could you help me out on this. Thank you

Are you trying to think of ways to get a random pet based on their rarities or get the percentage for the chances of each choice?

I’m trying to get the percentage for the chances of each pet so that I can display it on a gui whenever a player comes near one of the eggs.

In that case you could just use the rarities themselves because (75 + 24 + 1) is 100 so 75 is 75% of 100 and etc. Or maybe you’re looking for something different?

1 Like

I noticed the same but my problem is that those are the chances for the rarity meaning that there is a 75% chance to get a common, but common has a table of pets and I need the script to calculate how much of a chance there is to get a dog for example. rather than getting a common pet if that makes sense. Sorry I’m not the best at explaining

Later you do the rarity chance you should specify the table of pets and then you get the length of the table
(If its a table then it would be math.random(1, #table))
(If its a folder or something that has childrens then its math.random(1, #theThing:GetChildren()) )

So basically you would do something like this

local theChosenPet = math.random(1, #table)

In you’re code I don’t see anything that gives certain pets in a tier a chance to be chosen, if I am mistaken can you point it out to me?

1 Like

This function chooses a random one everytime activated

1 Like

The code you have there does not necessarily get random pets based on weight(chance), so the percentage of certain pets to be obtained cannot be calculated. Again if I am wrong please point out the line(s) of code that achieve that.

So the code, when fired gets a “randomnum” from 1,100 and if the “number” is lower than or equal to the random number it chooses a random pet from the table( it goes through the rarity table and checks if thare are any pets in that rarity. If there is it chooses a random one), you could do something like

local pet = petModule.choosepet()
print(pet.Name.." chosen")

and the output would be something like “Doggo chosen”
again sorry not the best at explaining

Again I do not see how you are getting a random pet from a tier based on chance, all I see is it getting a random number from the choices of pets in that tier and returning the value of the index of that tier. In my knowledge I don’t think there is a way to get percentages of the chances for it to return each choice given to math.random().

We could surely help you set up a way to put chance on certain pets in a tier.

1 Like