Loop doesn't return total value?

I have made a function that loops through players’ pets, which should technically add up the total amount of money (multipliers) for all pets, then return it.

For some reason, it only returns the value from a single pet (which it isn’t supposed to), even though there are lots of pets in the inventory and it loops through all of them.

function DataService:CalculateBuff(player, class)
	local MoneyBuff = 0
	local Inventory = PlayerSessionData[player].Pets.Inventory
	for _, GUIDs in pairs(Inventory) do -- loops through all pets (Ids)
		for _, petType in pairs(GUIDs) do --gets the pet type and should add value.
			MoneyBuff = PetModule[petType].Buffs.Money + PetModule[petType].Buffs.Money
		end
	end
	if class == "Money" then
		return MoneyBuff
	end
end
1 Like

How many pets are there? Are there any other code we need to know about? Are there images that might help us in solving your problem?

2 Likes

Because you’re assigning it to 2*the last pet’s money.
Add that to MoneyBuff if you want to sum it (MoneyBuff = MoneyBuff + PetModule[petType].Buffs.Money)

1 Like