Issues with a MouseButton1Click event inside a MouseButton1Click event

im currently making a store buy system which shows up a verification screen when you click buy. The mousebutton1click event is in the mousebutton1click event for the button that brings up the verification screen. When you join for the first time and buy one item from the store, it works fine, but when you buy the second item from the store and click the buy button on the verify screen, it re-buys the first one you bought and then buys the new one.

here is the whole function which creates the items in the store and then makes the events for clicking the buttons, the area where im having issues is at the bottom

function createItemInventory(plr, buyableitems)
	for i,v in pairs(buyableitems) do
		local crateType
		if table.find(gameData.Crates.Common, v) ~= nil then
			crateType = "Common"
		end
		if table.find(gameData.Crates.Uncommon, v) ~= nil then
			crateType = "Uncommon"
		end
		if table.find(gameData.Crates.Rare, v) ~= nil then
			crateType = "Rare"
		end
		if table.find(gameData.Crates.Epic, v) ~= nil then
			crateType = "Epic"
		end
		if table.find(gameData.Crates.Legendary, v) ~= nil then
			crateType = "Legendary"
		end
		if table.find(gameData.Crates.Mythical, v) ~= nil then
			crateType = "Mythical"
		end
		
		local costCoins = gameData.ItemPrices[crateType].Coins
		local costGems = gameData.ItemPrices[crateType].Gems
		
		--GUI stuff
		local storeframe = plr.PlayerGui.ScreenGui.storeFrame
		local itemframe = storeframe.BaseBox:Clone()
		itemframe.Parent = storeframe.items
		
		itemframe.coinbuy.Text = costCoins.." Coins"
		itemframe.gembuy.Text = costGems.." Gems"
		itemframe.ItemName.Text = v
		itemframe.crate.Text = crateType
		
		--cratetype color
		local cratecolor = storeframe.crates[crateType].info.BackgroundColor3
		itemframe.crate.BackgroundColor3 = cratecolor
	
		itemframe.Name = v
		
		
		--viewport
		local tooltype = findtooltype(v)	
		if tooltype == "Swords" or tooltype == "Slingshots" then
			local ViewportModel = game.ServerStorage.Weapons[tooltype]:FindFirstChild(v)
			if ViewportModel then
				local ViewportModelClone = ViewportModel:Clone()
				local ViewportFrame = storeframe.items[v].ViewportFrame
				ViewportModelClone.Parent = ViewportFrame
				for i,v in pairs (ViewportModelClone:GetChildren()) do
					if v.Name ~= "Handle" and v.Name ~= "Mesh" then
						v:Destroy()
					end
				end
				local ViewportCamera = Instance.new("Camera")
				ViewportFrame.CurrentCamera = ViewportCamera
				ViewportCamera.CFrame = ViewportModel.Handle.CFrame + (ViewportModel.Handle.CFrame.ZVector * Vector3.new(0,0,0))
				ViewportCamera.CFrame = CFrame.new((ViewportCamera.CFrame.Position + Vector3.new(2.5,0,0)) , ViewportModel.Handle.Position)
			end
		end
		if tooltype == "Kill Effects"then
			local img = game.ServerStorage.killEffects:FindFirstChild(v).ImageLabel
			local imgFrame = storeframe.items[v].ImageLabel
			imgFrame.Visible = true
			imgFrame.Image = img.Image
		end
		if tooltype == "Trails"then
			local img = game.ServerStorage.Trails:FindFirstChild(v).ImageLabel
			local imgFrame = storeframe.items[v].ImageLabel
			imgFrame.Visible = true
			imgFrame.Image = img.Image
		end
		
		local verify = plr.PlayerGui.ScreenGui.verifyItemBuy
		itemframe.coinbuy.MouseButton1Click:Connect(function()
			local plrcoins = plr:GetAttribute("Coins")
			if plrcoins >= costCoins then
				verify:TweenPosition(UDim2.new(0.3,0,0.3,0))
				verify.buy.Text = "Buy for ".. costCoins.." coins"
				verify.text.Text = "Are you sure you would like to buy "..v.." for "..costCoins.." coins?"
				verify.buy.MouseButton1Click:Connect(function()
					print(v)
					--addItemToInventory(plr, v)
					--updateHowManyLeftGUIAndCanBuy(plr)
					verify:TweenPosition(UDim2.new(0.3,0,1.3,0))
					--plr:SetAttribute("Coins", plrcoins-costCoins)
					itemframe:Destroy()
					--updateinvevent:Fire(plr, tooltype)
				end)
			else
				local textbefore = itemframe.coinbuy.Text
				itemframe.coinbuy.Text = "Not enough coins"
				task.wait(1)
				itemframe.coinbuy.Text = textbefore
			end
		end)
		itemframe.gembuy.MouseButton1Click:Connect(function()
			local plrgems = plr:GetAttribute("Gems")
			if plrgems >= costGems then
				verify:TweenPosition(UDim2.new(0.3,0,0.3,0))
				verify.buy.Text = "Buy for ".. costGems.." gems"
				verify.text.Text = "Are you sure you would like to buy "..v.." for "..costGems.." gems?"
				verify.buy.MouseButton1Click:Connect(function()
					addItemToInventory(plr, v)
					updateHowManyLeftGUIAndCanBuy(plr)
					verify:TweenPosition(UDim2.new(0.3,0,1.3,0))
					plr:SetAttribute("Gems", plrgems-costGems)
					itemframe:Destroy()
					updateinvevent:Fire(plr, tooltype)
				end)
			else
				local textbefore = itemframe.gembuy.Text
				itemframe.gembuy.Text = "Not enough gems"
				task.wait(1)
				itemframe.gembuy.Text = textbefore
			end
		end)
	end
end

fixed it by giving the event a name/variable/whatever then using :Disconnect()

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.