Is there a way to fire to the MouseButton1Click event?

Recently, I implemented a cursor to navigate menus with for controllers/gamepads. The problem with this is since I can’t directly fire MouseButton1Click on buttons, I must set the object that the cursor is hovering over to the SelectedObject, which forces the player to have to press A twice (once to select the object, and another to click on the object) to click on buttons in the menu.

Example of the problem:

So, is there a way to fire the MouseButton1Click event or somehow access the function connected to MouseButton1Click?

1 Like

Read the post instead of the title, please.

1 Like

I would recommend checking if the button has the “Selected” property to true then using UserInputService, check if the player has used ButtonA. Example:

local PressedOnce = false
local PressedTwice = false

UIS.InputBegan:Connect(function(inpt, gpe)
    if gpe then
        return
    end
    if inpt.KeyCode == Enum.KeyCode.ButtonA then
        if PressedOnce == true then
            -- // open function here
            PressedTwice = true
            PressedOnce = false
            return
        end
        PressedOnce = true
        spawn(function()
            if not PressedTwice then PressedOnce = false end
        end)
    end
end)

But that would require me to have a “pool” of all of the functions connected to the buttons, do I really need to resort to that?

1 Like

I don’t think so, but what you could do is create your own function and call it with parameters for the specific UI for MouseButton1Down and GamepadInput

1 Like

On Console, all UI is selectable to my knowledge, as this is how most people are able to use buttons, pressing the back button to select then pressing A to confirm.

I’m not checking if it’s selectable.

As a workaround, try creating a function to it and call the function instead. So that your only input is not through MouseButton1Click.

function myFunc()

end

-- event
button.MouseButton1Click:Connect(myFunc)

-- calling
myFunc()

I usually keep all my button functions in a module anyway so I can access them easily, I recommend doing this because then you can keep your code tidy.

2 Likes

Alright, but what would I do for buttons that use the same function? Create a separate, hard coded function for each of them? (Example: the HOME and CUSTOMIZE tabs in the gif in the OP)

Button1.MouseButton1Clicked:Connect(module.OpenMenu1)
Button2.MouseButton1Clicked:Connect(module.OpenMenu1)

Is this what you mean?

I mean buttons where their functions are created in a loop that references the current button.

Ah, easy. Just check if an argument was added for the button.

v.MouseButton1Click:Connect(function()
    module.Function1(v)
end)

-- // in module

module.Function1 = function(button)
    if button then
        -- // will print if the button argument was given
    end
end

Okay, I guess I’ll use this method. Thanks!