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
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
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:
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?
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()) )
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.