Duplicate system

Hello, I have script that adds item to player’s inventory. I want it to give player 300 cash and delete that duplicate from player’s inventory if it choses duplicate of item

script:

function module.buyhat(plr, hatshop, amount)
	if hatshop == nil then return end
	
	local price = hatshop:WaitForChild("Price").Value
	if plr:WaitForChild("stats"):WaitForChild(currency.Value).Value >= price then
		if #plr:WaitForChild("Items"):GetChildren() + amount > script:WaitForChild("Settings"):WaitForChild("MaxHatStorage").Value then return "Not enough space" end
		plr:WaitForChild("stats"):WaitForChild(currency.Value).Value = plr:WaitForChild("stats"):WaitForChild(currency.Value).Value - price
		local hatsOpened = {}
		
		for i = 1, amount do
			local rarity = getRandomRarity(hatshop)
			local hat = getRandomHatByRarity(rarity)
			
			if plr:WaitForChild("Items"):FindFirstChild(hat.Name) then
				-- how to do that if this happens do delete duplicate
                --from player's inventory and gives player 300 cash
			end
			
			local hatAdd = Instance.new("BoolValue",plr:WaitForChild("Items"))
				hatAdd.Name = hat.Name
				hatAdd.Value = false
			local rarit = Instance.new("StringValue",hatAdd)
				rarit.Name = "Rarity"
				rarit.Value = rarity
			local imageid = Instance.new("NumberValue",hatAdd)
				imageid.Name = "ImageID"
				imageid.Value = game.ServerStorage:WaitForChild("Items"):WaitForChild(hat.Name):WaitForChild("ImageID").Value

			
			table.insert(hatsOpened, hatAdd.Name)
			table.insert(hatsOpened, rarit.Value)
			table.insert(hatsOpened, imageid.Value)
			
		end
		
		return hatsOpened
	else
		return "Insufficient Funds"
	end
end

regards

Do you want to replace the original item or keep it and drop the duplicate instead? If you wish to replace it, you can delete the original item, and then parent the duplicate to the folder, and then give 300 cash. Otherwise, just skip the first part completely, and just give 300 cash.

@TestyLike3 ,I want it to kept dropping, but deleting the duplicate from client’s inventory and giving 300 cash