Mouse event not disconnecting despite disconnection

Hi there, I have a piano with tuning knobs that you can rotate to adjust a soung effect (e.g CompressorEffect). The issue is, is that the event mouse.Move does not disconnect despite it being assigned a variable, and disconnected. I’ve tried everything, and even used inputEnded to disconnect the function.

Script below:

for t,k in pairs(v) do
        local connection
        local connection2

        local turnclone = panelclone['turn']:Clone()
        turnclone.Name = k
        turnclone.TextLabel.Text = k
        turnclone.Visible = true

        local btn = turnclone.ImageButton

        turnclone.Parent = panelclone

        connection2 = btn.MouseButton1Down:Connect(function()
            connection = mouse.Move:Connect(function(x,y)
                local differenceX = btn.AbsolutePosition.X - mouse.X
                local differenceY = btn.AbsolutePosition.Y - mouse.Y

                local angle = math.deg(math.atan(differenceY/differenceX))
                btn.Rotation = angle
            end)
        end)

        uis.InputEnded:Connect(function(input)
            if input.UserInputType.Name == 'MouseButton1' and connection then
                connection:Disconnect()
                connection2:Disconnect()
                turnremote:FireServer(turnclone.Name,btn.Rotation)
            end
        end)
    end

Every time you are pressing your button down (MouseButton1Down), you are creating a new connection, and de-referencing (not removing or disconnecting) the previous connection.

Instead of doing…

connection2 = btn.MouseButton1Down:Connect(function()
	connection = mouse.Move:Connect(function(x,y)
		local differenceX = btn.AbsolutePosition.X - mouse.X
		local differenceY = btn.AbsolutePosition.Y - mouse.Y

		local angle = math.deg(math.atan(differenceY/differenceX))
		btn.Rotation = angle
	end)
end)

do this:

local connection
local connection2
local connection3

local isPressed = false

.......

connection3 = btn.MouseButton1Up:Connect(function()
	isPressed = false
end)

connection2 = btn.MouseButton1Down:Connect(function()
	isPressed = true
end)

connection = mouse.Move:Connect(function(x,y)
	if isPressed then
		local differenceX = btn.AbsolutePosition.X - mouse.X
		local differenceY = btn.AbsolutePosition.Y - mouse.Y

		local angle = math.deg(math.atan(differenceY/differenceX))
		btn.Rotation = angle
	end
end)

I don’t think you have to disconnect the two variables if you want the knobs to function again.

I’ll mark this as a more practical solution, however I got mine working by just setting the connection to nil after it was disconnected.

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