Roblox egg odds and a pity system in Luau
Short answer
Roll from weights, so each pet's chance is its weight divided by the total. For pity, save a counter per egg of hatches since the last rare; when it reaches your limit, roll among the rare pets only, and reset the counter whenever a rare comes out. Apply luck by multiplying only the rare weights, and build the odds players see from the same function the roll uses.
- In ServerStorage, add a ModuleScript named EggRoller and paste the first block.
- Call it from your hatch code as in the second block, and save profile.pity with the player's data.
- Paste the third block into a Script and press Run to see listed odds against 100,000 real rolls.
- Show both numbers to players: the chance per hatch, and the hatch by which a rare is guaranteed.
Copy-paste code
ServerStorage/EggRoller (ModuleScript)
local EggRoller = {}
-- `rare = true` marks the pets that luck boosts and that pity guarantees
EggRoller.Eggs = {
Starter = {
pityAt = 60, -- hatch number 60 without a rare is always a rare
pets = {
{ id = "Cat", weight = 600 },
{ id = "Dog", weight = 300 },
{ id = "Fox", weight = 90 },
{ id = "Dragon", weight = 10, rare = true },
},
},
}
-- The pets one roll can land on, with their weights. During a pity roll only the rares are in the pool.
local function pool(egg, luck, pity)
local entries, total = {}, 0
for _, pet in egg.pets do
if not pity or pet.rare then
local weight = if pet.rare and not pity then pet.weight * luck else pet.weight
table.insert(entries, { pet = pet, weight = weight })
total += weight
end
end
return entries, total
end
-- The chance of each pet on a normal roll, e.g. { Cat = 0.6, Dog = 0.3, Fox = 0.09, Dragon = 0.01 }.
-- Build the odds board from this, with the same luck value the roll will use.
function EggRoller.odds(eggName, luck)
local entries, total = pool(EggRoller.Eggs[eggName], luck or 1, false)
local out = {}
for _, entry in entries do
out[entry.pet.id] = entry.weight / total
end
return out
end
-- state: a table saved with the player, { sinceRare = number }
-- rand: a number in [0, 1), e.g. Random.new():NextNumber()
function EggRoller.roll(eggName, state, luck, rand)
local egg = EggRoller.Eggs[eggName]
state.sinceRare = (state.sinceRare or 0) + 1
local pity = egg.pityAt ~= nil and state.sinceRare >= egg.pityAt
local entries, total = pool(egg, luck or 1, pity)
local r = rand * total
local picked = entries[#entries].pet
for _, entry in entries do
r -= entry.weight
if r < 0 then
picked = entry.pet
break
end
end
if picked.rare then
state.sinceRare = 0
end
return picked.id, picked.rare == true, pity
end
return EggRollerUsing it from your hatch code (server)
local PolicyService = game:GetService("PolicyService")
local ServerStorage = game:GetService("ServerStorage")
local EggRoller = require(ServerStorage:WaitForChild("EggRoller"))
local rng = Random.new()
-- Some countries do not allow paid random items. Check before you sell an egg for Robux.
local function paidEggsAllowed(player)
local ok, info = pcall(PolicyService.GetPolicyInfoForPlayerAsync, PolicyService, player)
return ok and not info.ArePaidRandomItemsRestricted
end
-- profile = this player's saved data table
local function hatch(player, profile, eggName)
profile.pity = profile.pity or {}
profile.pity[eggName] = profile.pity[eggName] or { sinceRare = 0 }
local luck = if player:GetAttribute("LuckBoost") then 2 else 1
local petId, isRare, wasPity = EggRoller.roll(eggName, profile.pity[eggName], luck, rng:NextNumber())
return petId, isRare, wasPity
end
return { hatch = hatch, paidEggsAllowed = paidEggsAllowed }Odds check: 100,000 rolls
-- Paste into a Script and press Run: it rolls 100,000 eggs and prints what really came out.
local EggRoller = require(game:GetService("ServerStorage"):WaitForChild("EggRoller"))
local rng = Random.new(42)
local ROLLS = 100000
local counts, state, longestDrought, drought = {}, { sinceRare = 0 }, 0, 0
for _ = 1, ROLLS do
local petId, isRare = EggRoller.roll("Starter", state, 1, rng:NextNumber())
counts[petId] = (counts[petId] or 0) + 1
drought = if isRare then 0 else drought + 1
longestDrought = math.max(longestDrought, drought)
end
for petId, chance in EggRoller.odds("Starter", 1) do
print(string.format("%-7s listed %5.2f%% rolled %5.2f%%", petId, chance * 100, (counts[petId] or 0) / ROLLS * 100))
end
print("Longest run without a rare:", longestDrought)What pity does to the real odds
With the table above, the Dragon is listed at 1% per hatch. Without pity, a player has about a 55% chance of still having no Dragon after 60 hatches. Pity at 60 removes that tail: the longest possible wait is 59 hatches without one.
It also raises the real rate. With chance p per hatch and pity at hatch N, the long-run share of rares is p divided by (1 minus (1 minus p) to the power N). For 1% and 60 that is about 2.2%, which is what the odds check prints. So the honest label is "1% per hatch, guaranteed by hatch 60", not just "1%".
Luck without breaking the odds board
A luck boost should make rares more likely, not reshuffle everything. Multiplying only the rare weights does that: with luck 2 the Dragon goes from 10 in 1,000 to 20 in 1,010, just under 2%, and every other pet keeps its place.
EggRoller.odds takes the same luck value as the roll, so a player with a boost sees boosted odds. One function feeds both the board and the roll, which is the only reliable way to keep them equal.
Rules that apply to paid eggs
If an egg can be bought with Robux, directly or through a currency bought with Robux, Roblox requires that players can see the real odds before they buy. In some countries paid random items are not allowed at all; PolicyService tells you per player, as in the second block. Read the current Roblox policy before you ship a paid egg.
Good to know
Keep the pity counter per egg, not per player. A counter shared between a cheap egg and an expensive one lets players build pity on the cheap egg and spend it on the expensive one.
Pass the random number in, as this module does, instead of creating it inside. Then a test can feed fixed numbers and check the exact pet that comes out.
The counter must be saved. If it lives only in memory, leaving the game resets a player's progress towards the guarantee.
Roblox is a trademark of Roblox Corporation. CyberMax is not affiliated with or endorsed by Roblox.
Start with the free sampler
Before you buy anything, take the free Hatchwren sampler: two real modules from the kit (Formation, which places pets around the player, and Signal) with the .rbxmx to drag into Studio. If it fits how you work, the full kit is below.
Hatchwren Pet & Egg System: Pets and eggs for your simulator, done right
from $12Hatchwren ships these rules tested: shown odds that match the roll, luck, pity per egg, a triple hatch that charges once, Robux eggs, a saved pet inventory and Golden and Rainbow fusing.
Secure checkout · 14-day fix-or-refund if it doesn't work as described (terms)Works well with
Questions
What is a soft pity?
A rare chance that climbs with each miss instead of jumping to 100% at one hatch. To add it, multiply the rare weights by a factor that grows with state.sinceRare, and show that rule to players.
Does pity make the egg too generous?
It raises the average rate, by an amount you can calculate with the formula above. If that is more than you want, lower the base weight of the rare or move the pity number up, then run the odds check again.
Why did 100,000 rolls not match the listed odds exactly?
Random results always wobble a little; at 100,000 rolls the common pets should be within a fraction of a percent. The rare pet will sit above its listed chance when pity is on, for the reason explained above.
Should a pity hatch give the rarest pet?
That is a design choice. This module rolls among all pets marked rare by their own weights. To guarantee one specific pet, mark only that pet as rare.
Free: the Roblox Starter Kit
7 free Luau modules (damage, pets, shop formatting, admin audit log, tycoon guards and more) plus 3 free calculators, then 5 short emails with the scripting fixes we see go wrong most often.