Connection not disconnecting dispite printing "Connection"

I don’t understand what is going on. The first connection disconnects just fine, and I set up the second connection the same way yet it won’t disconnect. It will print “Connection” though.

local connection = nil
local scannedpart = nil
local connect22 = nil

script.Parent.deletebutton.MouseButton1Click:Connect(function()
	
	if canDeleteValue == true then
		
		StopFunctionValue = true
		canDeleteValue = false 
		script.Parent.deletebutton.Image = secondarytexture
		
		SelectionBox()--create the selection box
		
		
		
		connection = screenmouse.Move:Connect(function() --when the mouse moves, detect it
			Mouse.HitPosition()
			if truepart11.Parent.Parent.Name == "ItemStorage" then
				if truepart11.Parent.Parent.Parent.Name == Player.Name then
					s.Parent = truepart11.Parent
					s.Adornee = truepart11.Parent
					print(truepart11.Parent.Parent.Name)
					scannedpart = truepart11
					-----------------
					--Deciding on what part to delete
					local GuiName = scannedpart.Parent.Name
					local item = script.Parent.Parent.StructureFrame.Frame:WaitForChild(GuiName)
					local Energycost = item.EnergyCost.Value
					local Moneycost = item.MoneyCost.Value
					--
					connect22 = screenmouse.Button1Up:Connect(function()
						print(connect22) --WHERE THE ERROR OCCURS!!!!!!!!
						connect22:Disconnect()
					end)
				end
			end
		end)
	else 
		connection:Disconnect()
		script.Parent.deletebutton.Image = maintexture
		--end the delete function
		canDeleteValue = true
		StopFunctionValue = false
		s:Destroy()	
	end
end)

Help!

1 Like

This code is inside the mouse move event. Every frame you move the mouse, another one of these is created. This will create a memory leak since you create it every frame the mouse moves (and a few conditions).

I recommend moving some of your code around first.

1 Like

The main problem you’re seeing is that

connect22 = screenmouse.Button1Up:Connect(function()
	print(connect22) --WHERE THE ERROR OCCURS!!!!!!!!
	connect22:Disconnect()
end)

is setting the same variable every time your mouse moves over the desired target. Since you’re overriding the same variable every time you’re only keeping track of one of many connections. Ideally you wouldn’t continue to reconnect to the function over and over again, but one way to fix this is to wrap it with an if statement, like so:

if connect22 == nil then
	connect22 = screenmouse.Button1Up:Connect(function()
		print(connect22) --WHERE THE ERROR OCCURS!!!!!!!!
		connect22:Disconnect()
	end)
end

Alternatively you could also do something like this:

local connect22 = nil
connect22 = screenmouse.Button1Up:Connect(function()
	print(connect22) --WHERE THE ERROR OCCURS!!!!!!!!
	connect22:Disconnect()
end)

The downside to this method is that you’re still connecting to the MouseButton1Up event every time you move your mouse. Ideally you would separate all of these connections out so that you only need to connect to them once and just use variables to track state.

1 Like