Tool.Activated firing when pressing UI

Having a slight issue.

I’m designing an inventory UI system, however, Tool.Activated is firing, even though the user is pressing UI elements.

Is there a way to correct this behaviour without switching from Tool.Activated?

2 Likes

I don’t believe that there is. You can get around this issue, however, by using UserInputService and enabling Tool.ManualActivationOnly. Connect a listener to UserInputService.InputBegan that Tool:Activates the Tool if the correct input is inputted when it is equipped, and disconnect the connection when it is unequipped. We can use the second parameter of the listener to ignore inputs from UI elements:

local userInputService = game:GetService("UserInputService")
local connection

local function onInputBegan(input, isProcessed)
    if isProcessed then return end
    if input.KeyCode ~= Enum.KeyCode.MouseButton1 then return end
    tool:Activate()
end

tool.Activated:Connect(function()
    print("activated")
end)

tool.Equipped:Connect(function()
    connection = userInputService.InputBegan:Connect(onInputBegan)
end)

tool.Unequipped:Connect(function()
    connection:Disconnect()
end)
1 Like

Nice idea! Thank you. :slight_smile:

I’m not able to reproduce this issue. Could this potentially be a bug? I created a quick repro in Studio by creating a Tool with 3 lines of code and adding in a TextButton GuiObject.

-- Tool Code:
script.Parent.Activated:Connect(function ()
	print("activ")
end)

“activ” was printed when I clicked as expected of the Activated event, however it did not print when I clicked on the TextButton GuiObject. Perhaps it’s just the way I set up the repro, but I haven’t been able to yield the same results as you have.


Referencing the above: if you’re going to use UserInputService as a workaround, you might as well fully convert over rather than use the Activated event. UserInputService has sufficient support for both input beginning and ending. If you’re looking for a one-time activation, I would just use the PlayerMouse that is automatically passed by the Equipped event.

local Tool = whatever

Tool.Equipped:Connect(function (mouse)
    mouse.Button1Down:Connect(function ()
        -- whatever
    end)
end)

Just don’t fall into the trap of using mouse.KeyDown, since that’s a deprecated and bad way of capturing key input nowadays. UserInputService and ContextActionService have reasonably everything you’d need for input-based coding.

1 Like

I think your original issue is having the Active property disabled in your GuiObject.

5 Likes

This did the trick. Thanks.