CyberMax
CyberMax › Answers

Roblox egg hatching script: weighted random pets with real odds

Updated 2026-10-17 · Roblox and game developers · by CyberMax

Short answer

Give each pet in an egg a weight, roll a random number between 0 and the total weight on the server, and walk the list until the running total passes the roll. The odds you show players are each weight divided by the total, so they always match what the code does. Charge the coins and add the pet in the same server step.

Copy-paste code

Server: hatch one egg

local EGGS = {
	starter = { price = 100, pets = {
		{ id = "cat", weight = 60 },
		{ id = "dog", weight = 30 },
		{ id = "dragon", weight = 9 },
		{ id = "golden_dragon", weight = 1 },
	} },
}
local rng = Random.new()

local function roll(egg)
	local total = 0
	for _, p in egg.pets do total += p.weight end
	local r = rng:NextNumber() * total
	for _, p in egg.pets do
		r -= p.weight
		if r < 0 then return p.id end
	end
	return egg.pets[#egg.pets].id
end

local function odds(egg) -- for the egg's info board: { cat = "60%", ... }
	local total, out = 0, {}
	for _, p in egg.pets do total += p.weight end
	for _, p in egg.pets do out[p.id] = string.format("%g%%", p.weight / total * 100) end
	return out
end

game.ReplicatedStorage.Hatch.OnServerInvoke = function(player, eggId)
	local egg = EGGS[eggId]
	local data = profiles[player]
	if not (egg and data) or (data.coins or 0) < egg.price then return false end
	data.coins -= egg.price
	local pet = roll(egg)
	data.pets = data.pets or {}
	table.insert(data.pets, { id = pet, uid = game:GetService("HttpService"):GenerateGUID(false) })
	return true, pet
end

Good to know

Show the odds from the same table the roll uses (the odds() function), so they can never drift apart.

Give each hatched pet a unique id (uid) so trading, deleting and equipping always refer to the right copy.

Hatchwren Pet & Egg System: Pets and eggs for your simulator, done right

from $12

Hatchwren adds luck and pity, charge-once triple hatch, auto-delete, Robux eggs, a saved pet inventory and Golden/Rainbow fusing, tested.

Secure checkout · 14-day fix-or-refund if it doesn't work as described (terms)

Questions

Is math.random fine?

Random.new() is the recommended generator; both are fine for eggs.

How do I add luck boosts?

Multiply the weights of rare pets by the boost before rolling, and show the boosted odds.

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.

You get one email to confirm first. Then the kit, and 5 short emails over two weeks. One-click unsubscribe in every email; we never share your address.

Related answers

Roblox pet system: make pets follow the player smoothlyA client-side pet follow script with AlignPosition-free CFrame lerping, arc formation for several pets and a hHow to use developer products in Roblox (ProcessReceipt that never double-grants)Sell coins and boosts with developer products: the ProcessReceipt pattern that records each PurchaseId so a pu