Would this egg script work?

How could i make this code better/will it work

(its not done yet but what i have so far)

local eggModule = {}

local random = Random.new()

eggModule.Eggs = {
	["Common Egg"] = {
		Cost = {"Coins",500},
		Secrets = {"Giant Kitty","TV"},
		Rarities = {
			["Doggy"] = {["Chance"] = 3750000, ["Rarity"] = "Common",["Percent"] = "37.5"},["Kitty"] = {["Chance"] = 2750000, ["Rarity"] = "Uncommon",["Percent"] = "27.5"},
			["Bunny"] = {["Chance"] = 2500000, ["Rarity"] = "Rare",["Percent"] = "25"},["Bear"] = {["Chance"] = 988794, ["Rarity"] = "Epic",["Percent"] = "9.88"},
			["Deer"] = {["Chance"] = 10000, ["Rarity"] = "Legendary",["Percent"] = "0.1"},["Dragon"] = {["Chance"] = 1000, ["Rarity"] = "Legendary",["Percent"] = "0.01"},
			["Golem"] = {["Chance"] = 200 , ["Rarity"] = "Legendary",["Percent"] = "0.002"},["Giant Kitty"] = {["Chance"] = 5, ["Rarity"] = "Secret",["Percent"] = "5E-05"},
			["TV"] = {["Chance"] = 1, ["Rarity"] = "Secret",["Percent"] = "1E-05"}
		},
		Pets = {
			--Not done with all pets
			["Doggy"] = {game.ReplicatedStorage.Assets.Pets.}
		}
	}
}

function GetRarityTotalChance(eggToHatch)
	local total = 0
	for k, v in next, eggModule.Eggs[eggToHatch].Rarities do
		total += v["Chance"]
	end
	return total
end

function SetRandomMinMax(eggToHatch)
	local currentMin = 0
	local currentMax = 0
	for k, v in next, eggModule.Eggs[eggToHatch].Rarities do
		currentMin = currentMax + 1
		currentMax = currentMax + v["Chance"]
		eggModule.Eggs[eggToHatch].Rarities[k]["Min"] = currentMin
		eggModule.Eggs[eggToHatch].Rarities[k]["Max"] = currentMax
	end
end

eggModule.ChoosePet = function(eggToHatch)
	SetRandomMinMax(eggToHatch)
	local randomChance = random:NextInteger(1,GetRarityTotalChance(eggToHatch))
	
	for k, v in next, eggModule.Eggs[eggToHatch.Rarities] do
		if randomChance >= v["Min"] and randomChance <= v["Max"] then
			local rarity = v["Rarity"]
			local percent = v["Percent"]
			
			local rarityTab = eggModule.Eggs[eggToHatch].Pets[k]
			local chosenPet = rarityTab[1]
			
			return chosenPet, rarity, percent
		end
	end
end

return eggModule
1 Like

Please do your own testing. Code Review is for code that you know works and for feedback on specific parts of your code you feel are lacking and can be improved on. Finding and giving advice for parts of code is not what Code Review is for, nor checking if your code works. Recategorised out for now.

3 Likes