How it works

  • Each egg is a list of pets with weights. A pet’s chance is its weight divided by the sum of all weights.
  • The server takes the coins and rolls the pet. Never let the client choose: anything a client decides can be faked.
  • The client asks through a RemoteFunction, gets the pet’s name back and plays the hatch.

1. The egg and the roll (server)

Put this Script in ServerScriptService. It expects a leaderstats folder with a Coins value, the usual setup in pet games.

-- ServerScriptService > EggService (Script)
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local EGGS = {
	Starter = {
		price = 100,
		pets = {
			{ name = "Chick", weight = 50 },
			{ name = "Bunny", weight = 30 },
			{ name = "Ladybug", weight = 15 },
			{ name = "Phoenix", weight = 4.5 },
			{ name = "Inferno Dragon", weight = 0.5 },
		},
	},
}

local rng = Random.new()

local function rollPet(egg)
	local total = 0
	for _, pet in egg.pets do
		total += pet.weight
	end
	local roll = rng:NextNumber() * total
	for _, pet in egg.pets do
		roll -= pet.weight
		if roll < 0 then
			return pet.name
		end
	end
	return egg.pets[#egg.pets].name -- rounding safety
end

local hatchEgg = Instance.new("RemoteFunction")
hatchEgg.Name = "HatchEgg"
hatchEgg.Parent = ReplicatedStorage

hatchEgg.OnServerInvoke = function(player, eggName)
	local egg = EGGS[eggName]
	if not egg then
		return nil
	end
	local stats = player:FindFirstChild("leaderstats")
	local coins = stats and stats:FindFirstChild("Coins")
	if not coins or coins.Value < egg.price then
		return nil
	end
	coins.Value -= egg.price
	local petName = rollPet(egg)
	-- save petName to the player's inventory here (DataStore or your inventory module)
	return petName
end

With these weights the Inferno Dragon is 0.5%: one hatch in 200 on average. Try other numbers in the egg odds calculator; it also writes this table for you.

2. The hatch (client)

A LocalScript in StarterPlayerScripts. Pet models live in a ReplicatedStorage.Pets folder, named exactly like in the egg table. Call hatch from your egg’s ProximityPrompt or button.

-- StarterPlayerScripts > HatchClient (LocalScript)
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local hatchEgg = ReplicatedStorage:WaitForChild("HatchEgg")
local petModels = ReplicatedStorage:WaitForChild("Pets")

local function hatch(eggName, eggModel)
	local petName = hatchEgg:InvokeServer(eggName)
	if not petName then
		return nil -- not enough coins
	end
	local start = eggModel:GetPivot()
	for _ = 1, 3 do -- shake, shake, shake
		eggModel:PivotTo(start * CFrame.Angles(0, 0, math.rad(12)))
		task.wait(0.12)
		eggModel:PivotTo(start * CFrame.Angles(0, 0, math.rad(-12)))
		task.wait(0.12)
	end
	eggModel:PivotTo(start)
	local pet = petModels[petName]:Clone()
	pet:PivotTo(start)
	pet.Parent = workspace
	return pet
end

3. Show the odds

If the egg costs Robux, or a currency that can be bought with Robux, Roblox asks you to show every possible pet and its chance before the purchase. This turns the same table into text for a “Details” panel:

local function oddsText(egg)
	local total = 0
	for _, pet in egg.pets do
		total += pet.weight
	end
	local lines = {}
	for _, pet in egg.pets do
		table.insert(lines, string.format("%s: %.2f%%", pet.name, pet.weight / total * 100))
	end
	return table.concat(lines, "\n")
end

Rounded numbers have to add up to exactly 100%. The rules in full, and why “1 in 200” is not “once every 200 hatches”, are in the pet game odds guide.

4. Golden and Rainbow pets

Mutations are a second roll after the pet. Check the rarest first, so a lucky player gets the best one:

local MUTATIONS = { -- rarest first
	{ name = "Rainbow", chance = 1 / 3000 },
	{ name = "Golden", chance = 1 / 50 },
}

local function rollMutation()
	for _, mutation in MUTATIONS do
		if rng:NextNumber() < mutation.chance then
			return mutation.name
		end
	end
	return nil
end

Return petName, rollMutation() from the server and tint the clone on the client. For the inventory, Pet Factory makes the icons of every pet with Golden, Diamond, Rainbow and five more mutation looks.

Pets to put in the eggs

Our Roblox packs come as a drag-and-drop .rbxm with animations in code, rarity effects and both studs and smooth looks. Three of them are free under CC BY: Cube Pets Free.

Sources