Iteration not printing the same thing?

Iteration not printing the same thing? I iterate first time, it prints Dog and Demon. Second Time, it prints Dog only.

Can you send your code here please. Makes it easier for me to solve.


local function WeightedChance(EggName, plr)
	local GetSum = SumOfWeight(EggName, plr)
	local EggIndex = GetEgg(EggName)
	
	for i,v in pairs(EggModule[EggIndex].Pets) do
		print(v.PetName)
	end
	if GetSum > 0 and EggIndex then
		local random = math.random(GetSum)
		for i, v in pairs(EggModule[EggIndex].Pets) do
			local PetChance = v.Chance
			
			
			print(v.PetName)
			if random <= PetChance then
				local Fits = AllowedRarity(v.PetName)
				if Fits then
					Thread.Spawn(function()
						Knit.Services.PetService:GetHatched(plr, v.PetName, v.Chance, EggName)
					end)
				end
				return v.PetName
			else
				random = random - PetChance
			end
		end
	end
end

What does EggModule refer to?

Egg Index, and with some information about pets, currency (etc).

Is that a table??

Off course its a table. If it wasn’t it would error.

Could you print that table?

It looks like it’s printing Dog Demon on both iterations. First two prints refer to line 200 while the third refers to 208. After that, you have two more prints referring back to line 200 and then another on line 208.

Screen Shot 2021-04-27 at 10.45.22 AM

Yes both print Dog. But second iteration never prints Demon???

It appears that it is printing Demon on your second iteration.
image

Both of these prints come from line 200, which is your iteration print.

This is first one. Second one is line 208, this determines which pet it is at the end.

Your first iteration prints the entire table while the second iteration only prints Dog because you are returning Dog before iterating to Demon.