Problem with luck in egg hatching system

Hi developers, I was testing egg hatching system and I found weird issue. When Chance = 0 you can get pet which have 15% but you should to get pet with 0.8%

Screen shots:

Result:
image

Rare:
image

Legendary:
image

Script:

local function ChoosePet(egg)
	local Chance = math.random(0,100)
	local Counter = 0
	for _,pet in pairs(game.ReplicatedStorage.Pets[egg.Name]:GetChildren()) do
		Counter = Counter + pet.Rarity.Value
		if Chance <= Counter then
			print("Counter: ", Counter, "Chance: ", Chance)
			return pet.Name
		end
	end
end

this is because 0 is also <= 15%, and since 15% is the pet you FIRST check, it will obviously take that and exit out

would be a simple fix IF you had stored the value inside of a list, but i dont think i can help fix it if ur using the children objects of stuff (obviously thats cuz im too trash to do list stuff with children objects)

btw u just wanna somehow change the if statement to “if CounterOfNextPet < Chance <= Counter then”

but the problem here is getting the counter of the next pet (for example if u have 2 pets of 15% and 0.8% chance, you would first be checking the 15%, now since the “Chance” value is 0, it is NOT greator then the counter of the next pet (0.8%), therefore the if statement does not go throught and it would iterate to the 0.8%
However with this fix it is important to add a “-1%” as the last object, and have the for statement stop iterating before it hits the last object (so like, for_, pet in pairs(stuff) - 1) btw i dont know alot about for statements in LUA so that might be a syntax error XD

Traverse over the pet types in ascending order according to their values, for each check if the pseudo-randomly generated number is less than the value.