Picking a random pet by percent

I want to pick a random pet from the PetRarities Tabel but it has to be dependent on a percentage
for example, a legendary would be a 1 percent change so 1 in a 100 eggs would be a legendary
pet. This Is similar to the egg hatching system popular simulator games have.

	local Pets = {"Dog","Cat","Mouse","Dragon","GrassyCat","TeaMonster"}
		local PetRarties = {
			Common = 40; -- 1-40
			Uncommon = 30; --40-70
			Rare = 15;  -- 70-85
			Epic = 10; --85-95
			Legendary = 5 --95-100
			
		}
		local RandomPet = math.random(1,100)
		for i,v in pairs(PetRarties) do
			--print(i..v.."Chance")
			
		
			
			
			
			
			
		end
		``

Well the thing is your pets aren’t sorted based on rarity type, so there’s no way to know which pets are which.

Here’s what I would do:

local pets = {
Common = {"pet1"};
Uncommon = {"blah"};
Rare = {"Ahoy"};
Epic = {"Geno"};
Legendary = {"robert"};
}

local petChances = {
Common =  40;
Uncommon = 30;
Rare = 15;
Epic = 10;
Legendary = 5;
}

local function rollPet()
local chance = math.random(100)
local rarity, smallestChance;

for rarityName, rarityChance in pairs(petChances) do
if (not rarity and chance <= rarityChance) or ((chance <= rarityChance) and rarityChance < smallestChance) then
--//Find the rarest pet the player can have based on the randomly drawn number
rarity = rarityName
smallestChance = rarityChance

end
end

--//Now grab a random pet based on the rarity that was selected
end

Forgive the odd variable names, but hopefully it helps.

1 Like

Yes the script works thank you so much

1 Like