Basically I found an egg hatching tutorial and this is the chance script; Im having trouble understanding how rare each rarity actually is. Is there any way to convert these into percentages? ( without changing the code, i just want to have
--[[ Rarities; 1K = 25% 500 = 13% 300 = 6% 100 = 3% 1 = 1% --]]
Not those exact percentages but something along those lines
local module = { --Demonic ["Common"] = { ["Cost"] = 500, ["Pets"] = {-- {Weight/Rarity} ["Cat"] = {1000}, ["Dog"] = {1000}, ["Bunny"] = {300}, ["Bear"] = {300}, ["Rare Pet"] = {100}, ["Unobtainable Pet"] = {1}, }, } } return module --[[ Rarities; 1K = Common 500 = Uncommon 300 = Uncommon 100 = Rare 1 = Extreme --]]
and here is where the math.random is:
local function ChoosePet() local Chance = math.random(1,TotalWeight) local Counter = 0 for i,v in pairs(Pets) do Counter = Counter+v[1] if Chance <= Counter then return i end end end
here is the full script in case you need it;
local ProximityPromptService = game:GetService("ProximityPromptService") local RS = game:GetService("ReplicatedStorage") local Eggs = require(RS:WaitForChild("Eggs")) local Data = Eggs[script.Name] local Cost = Data["Cost"] local Pets = Data["Pets"] local Buy = game.Workspace.Eggs.Common local Cost1 = Buy:WaitForChild("Cost") local CD = Buy.Buy:WaitForChild("ProximityPrompt") local CostGUI = Cost1:WaitForChild("Am") local CostText = CostGUI:WaitForChild("TextLabel") CostText.Text = Cost.." Coins" local TotalWeight = 0 for i,v in pairs(Pets) do TotalWeight = TotalWeight + v[1] end local function ChoosePet() local Chance = math.random(1,TotalWeight) local Counter = 0 for i,v in pairs(Pets) do Counter = Counter+v[1] if Chance <= Counter then return i end end end CD.Triggered:Connect(function(Player) local Stats = Player:WaitForChild("leaderstats") local Coins = Stats:WaitForChild("Coins") local open = Player:WaitForChild('OpenValue') local got = Player:WaitForChild('GotPet') if Coins.Value >= Cost then Coins.Value = Coins.Value - Cost open.Value = 'Open' got.Value = ChoosePet() local findpet = Player:WaitForChild('Pets') local sqq = findpet:FindFirstChild(got.Value) sqq.Value = 'owned' wait(5) open.Value = 'Close' got.Value = '' end end)
Assuming you understand all of your code, its simple.
If you want to figure out the chance that you will get each item (as a percent), all you need to do is divide each value by the total of all values.
item | value |
---|---|
item1 | 1000 |
item2 | 2000 |
item3 | 500 |
item4 | 70 |
Here you have the values 1000, 2000, 500, and 70 if you add them all together you get 3570.
now to get the probability of item1
you divide 1000 by 3570, which is about 0.28011
converted into a percentage (multiply by 100) you get 28% chance of getting that value.
doing the same with item3
you get 0.14, which is 14%.
Now, we understand how to calculate percentage, but what if we know what percentage we want but not the value. First of all make sure all your percentages add up to 100.
item | percent |
---|---|
item1 | 25% |
item2 | 50% |
item3 | 12.5% |
item4 | 12.5% |
Now we can determine our resolution, what is resolution you ask? Basically its gonna be our total from the previous part. The most obvious choice is 100 but it can really be any integer bigger than our total (must be an integer).
First lets convert all of these percentages to decimal.
item | decimal |
---|---|
item1 | 0.25 |
item2 | 0.5 |
item3 | 0.125 |
item4 | 0.125 |
now simply multiply your resolution by your decimal
say our resolution is 500
item1
would be 500 * 0.25 which is 125
etc.