Sell system not working correctly

Hey, I’ve been working on a sell system recently and I’ve got it to function somewhat correctly however I’m noticing a problem that I haven’t been able to fix.

Basically, each ore in my game is worth a specific amount. Something like this:

local module = {
	Stone = {
		Name = "Stone",
		Price = 1 -- The set price of the ore
	},
	
	Coal = {
		Name = "Coal",
		Price = 2
	},
	
	Iron = {
		Name = "Iron",
		Price = 4
	},
}

return module

Then, when the player touches the sell part, I do this:

mainPart.Touched:Connect(function(hit)
	if cooldown == false then
		cooldown = true
		
		if hit and hit.Parent then
			local character = hit.Parent
			local player = Players:GetPlayerFromCharacter(character)
			
			if player then
				local inventory = player.Inventory
				local leaderstats = player.leaderstats
				local money = leaderstats.Money
				
				-- Having trouble down here
				for itemName, quantity in pairs(inventory:GetChildren()) do --// looping through players inventory
					local price = sellingPrices[quantity.Name] --// getting price of a single item
					local itemsValue = price * itemName --// getting the total price of users' items

					totalPrice = totalPrice + itemsValue --// adding it to the total value
					
					money.Value += totalPrice
					
					totalPrice = 0
					quantity.Value = 0
				end
			end
		end
		task.wait(2)
		cooldown = false
	end
end)

It should be printing 7 as I have gave myself 1 of each ore in my inventory. However, it prints 17. I would like some help because I have no clue what is going on and how to fix it. Any help would be appreciated!

1 Like

change this to quantity.Value

for itemName, quantity in pairs(inventory:GetChildren()) do --// looping through players inventory
	local price = sellingPrices[quantity.Name] --// getting price of a single item
	local itemsValue = price * quantity.Value --// getting the total price of users' items
	
	totalPrice = totalPrice + itemsValue --// adding it to the total value
	
	money.Value += totalPrice
	
	totalPrice = 0
	quantity.Value = 0
end