Can you make a custom event like .Activated that uses UserInputService?

I want to make a custom event similar to .Activated in order to connect GUI’s to functions. My problem with .Activated is that it needs ImageButton.Active = true in order to function. This .Active property blocks UserInputService events, like ScrollingFrame.InputBegan.

I presume object-oriented is the way here, but I don’t know much about metatables and metafunctions.

This is what I have:

function GuiModule.Click(Input)
    if Input.UserInputType == Enum.UserInputType.MouseButton1 or Input.UserInputType == Enum.UserInputType.Touch then
        return true
    else
        return false
    end
end
local GuiModule = require(game.Players.LocalPlayer.PlayerGui.GuiModule)

GuiButton.InputEnded:Connect(function(Input)
    if GuiModule.Click(Input) then --I really have to write an if statement for every InputEnded event?
        print("Button clicked")
    end
end)

This is my ideal solution, where I can choose which UserInputService buttons and keys activate .Clicked on various platforms:

GuiButton.Clicked:Connect(function(Input)
    print("Button clicked")
end)

.MouseButton1Click will fire even if .Active is false. Although it says “Mouse”, it also reacts to touchscreen taps.

You cannot attach custom events to Roblox instances. You can write a function using other existing events as a workaround, but .MouseButton1Click will probably get you what you want much quicker.

2 Likes