MouseButton1Click Not Being Detected

Good day all,

I have a module script which has the function AddButton. This function manages buttons within my game. This particular function will perform a given function using given function arguments.

For some reason, MouseButton1Click is not being detected when operating the script. No errors are generated, and all other logic performs as anticipated. The print function within the anonymous function does not fire.

function UIManager.AddButton (Element, Indent, Function, FunctionArguments)
	-- variable assignment
	local InitialPosition = Element.Position
	-- connect hover event
	Element.MouseEnter:Connect (function()
		-- tween transparency
		local Elements = Element:GetDescendants()
		Elements[#Elements + 1] = Element
		UIManager.ModifyTransparency(Elements, Configuration["Hover Transparency"])
		-- tween indent (if applicable)
		if Indent ~= nil then
			UIManager.ModifyPosition(Element, InitialPosition + (0, Indent, 0, 0), "InOut")
		-- connect click event
		local ClickConnection = Element.MouseButton1Click:Connect (function()
			-- perform Function using FunctionArguments
			print("The button has been selected!")
			Function (FunctionArguments)
            -- prevent future selections
            UIManager.RemoveButton (Element)
			return nil
		-- wait for unhover
		-- disconnect click event
		-- tween transparency
		UIManager.ModifyTransparency (Elements, Configuration["Unselected Transparency"])
		-- tween indent (if applicable)
		if Indent ~= nil then
			UIManager.ModifyPosition(Element, InitialPosition, "InOut")

I have attempted workarounds for this issue, I’ve attempted to use GuiObject.InputBegan() and GuiButton.Activated() but a similar issue has arisen for both of these.


It’s looking like your event is being immediately disconnected here as soon as Element.MouseLeave is fired. I’m also not sure why you have Disconnect there, try removing it and seeing if it fixes your issue.


However the mouse has to be hovering over the button to click the button, so we can say for certain Element.MouseLeave hasn’t fired yet.
I have my reasons for disconnecting the event after the button is left but for the sake of clarity I’m not going to explain why.

1 Like

I’d throw a print just before the disconnection to check it isn’t disconnecting early due to a bug with MouseLeave firing during a click or something.

I’d also drop a print just before the connection to ensure the script has actually added the connection before you click.

1 Like

Both function as intended.
Filler text because 30 character limits.

1 Like

That goes without saying as the person has to physically move their cursor over the button to click it.

Please do explain why, as it would help me understand why you aren’t refactoring your code a different way

1 Like

Could you replace this line with:

-- connect click event 
local ClickConnection; ClickConnection = Element.MouseButton1Click:Connect (function() 
    -- perform Function using FunctionArguments

I’ve had issues with connections in ModuleScripts before and this seemed to fix them.

1 Like

What does that do?
Filler text for text limit.

I don’t really know how to explain it, but I’ve only want the function to run if it’s not currently selected as an option, so I don’t even know what I’m saying here but I have a plan and it’ll probably change. It has little to do with the fact the issue hasn’t been resolved.

Just add a flag then. When you select an option, change an internal variable such as “SelectedOption” to the one that was selected or have some way to indicate what is selected. Then in your mouse hovering code, if the option is selected, don’t do anything.

Your wording is confusing and/or contradicting to what your code shows and I’m not really sure what you’re trying to achieve here. There seems to be a code smell in the terrible way that the buttons are being handled. Click is connected from inside MouseEnter, for example. Separate all of that.

-- FunctionArguments can just be replaced with varargs
function UIManager.AddButton(Element, Indent, Function, ...)
    local InitialPosition = Element.Position
    local HoveredOver = false

    -- All below is simplified for understanding's sake; I do not code this way

    -- Account for hovering using one event
    Element.InputChanged:Connect(function (inputObject)
        -- Confirm the input sent was from the mouse
        if inputObject.UserInputType == Enum.UserInputType.MouseMovement then
            if inputObject.UserInputState == Enum.UserInputState.Begin then
                HoveredOver = true
                -- Hover code goes here
                HoveredOver = false
                -- Unhovering code goes here

    -- Account for mouse button presses using another function
    Element.InputBegan:Connect(function (inputObject)
        if inputObject.UserInputType == Enum.UserInputType.MouseButton1 then
            if HoveredOver then
                -- Pass varargs as function arguments
                -- If func needs table, use table.pack or {...} to marshall args

Don’t disconnect events where not necessary and if possible, don’t connect event listeners inside of event listeners unless it’s necessary for that to happen. Handle your functions independently and individually. Code above; up to you to fill the rest out and test.

1 Like

If you’re encountering an issue which doesn’t make any sense whatever and appears to be some description of Studio bug, it’s almost certain to be an issue with your explorer setup. In this case, due to some restructuring, I had an ImageButton parented to the TextButton whose click I was attempting to collect. However, because of how these buttons overlapped, the ImageButton was stealing the click detections.

Whenever posting your assistance requests, make sure to include relevant screenshots of the explorer in case it is such a technicality of the client. Thanks.


The overlapping GuiObject in this case would be stealing the click detection only if you aren’t using Sibling ZIndexBehaviour and the ImageButton has a higher ZIndex than the TextButton, which would make sense for styling buttons.

Posting an explorer screenshot isn’t necessarily enough to determine a problem. If someone asked about your ZIndex behaviour then that could identify the problem but sometimes it isn’t organisation and it’s actually your code (which, by the way, you’ve got some spaghetti code going on there).

1 Like

I determined it was either a bug or an issue unrelated to the script because I had tried executing the click event on it’s own within the local script from which the module script is called, and it still remained incapable of detecting clicks.

The entire purpose of putting an explorer screenshot is to help identify the issue if it is an issue with the set-up of your explorer. If you’ve got an issue with people providing more detail for you to work on, then look upon this post an example to the contrary.

I don’t think you read my response at all, please read it again. It outlines very clearly what could have been causing your problem. In this case, an explorer screenshot does nothing. It’s either the result of spaghetti code (which your code is very close to) or ZIndex behaviour (which is what you outlined).