How to disconnect MouseEnter, MouseLeave

my brain forgot to remove a example post thing

  1. I want to disconnect MouseEnter and MouseLeave so it doesn’t activates anymore

  2. The issue is, disconnecting it, doesn’t seem to actually disconnect it.
    It still prints whatever I set, even if it shouldn’t

  3. I tried

local something
something = v.MouseEnter:Connect(function() -- v is Image Button
print("test")
end)
if typeof(something) == "RBXScriptConnection" then -- In auto-fill (idk how to name it), it shows as a RBXScriptConnection
something:Disconnect()
print("Disconnected?", something) -- It does prints, but it does not disconnects?
end

I also tried

local something
something = v.MouseEnter -- v is still same (Image Button)
something:Connect(function()
warn("test")
end)
something:Disconnect()
-- But it gives an error: Disconnect is not a valid member of RBXScriptSignal

Ik why it errors at last, cuz its not rbxscriptconnection, but uh

I don’t think you need to compare something to RBXScriptConnection when trying to disconnect it. If you already know that this variable is going to be used for and only for a connection, this step is redundant. Usually what I do is just check if a connection exists, but in order to do that I always add one small step when disconnecting a function.

local something
something = v.MouseEnter:Connect(function()
    print("test")
end)

--disconnect something
if something then
    something:Disconnect()
    something = nil --Setting 'something' to nill ensures that it no longer exists allowing me to compare it in an "if" statement to see if it exists.
end
1 Like

I not sure why it happens, but, if I just try to disconnect it right after it was connected, it works.

But if I do it when player does something, it doesn’t disconnects it, however I added a if statement and print after it.
It does print, meaning it should disconnect it, but it does not
image

image

image

This seems correct since you’re referencing to something after you disconnected and set it to nil, so its printing nil.

But I can’t really tell you what the issue is without more context. Could you provide me a larger snippet of your code? This should be disconnecting without issue. As a test, create a brand new script and just make one connection and attempt to disconnect it after a certain action and see if it persists.

it says its nil, but it still printing when moved mouse on it.

I think I will just store data of item (Because it will have to change when player equips another item in that exact slot) inside of image button since that thing doesn’t disconnects and it seems for me only option

Still thanks for trying to help

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