Gui inside a plugin cannot detect clicking a button

Is it just me or is it not working for the client? Anyways moving on.


What I tried to achieve?

Hello there I was going to make a plugin that if you click on the button on the widget, it generates a new script. But I was stuck to this problem that you can't click a button.

What I tried to do

I created a demo script that is inside a textbutton and used it to print “Hello World!” in the console when clicked. Here is my 2 functions I tried.

Heres my first with mousebutton1click.

script.Parent.MouseButton1Click:Connect(function()
    print("Hello World!")
end)

And then tried again, but with a different function.

script.Parent.Click:Connect(function()
    print("Hello World!")
end)

Any help is appreciated!

I’m pretty sure the issue is that the code listening for mouse input is never being run. This is because the code exists in a script that is parented to the gui object. Try referencing the button from the main script of your plugin. Might look something like this:

--This is you main plugin script, the one that creates the toolbar and button
local WIDGET_MAIN = script:WaitForChild("WidgetMain") --or whatever your gui is named

--Then you can create your DockWidgetPluginGui and parent WIDGET_MAIN to it

WIDGET_MAIN.Button.MouseButton1Click:connect(function()
    print("Hello, World!")
end)

(Later on I recommend separating all code that handles gui input and functionality to a ModuleScript which can be parented to and required by the main plugin script)
~Finding this fairly difficult to describe. Let me know if I need to clarify something

1 Like

Add this as the first line of code in your GUI. If this fixes the issue, it means it wasn’t active, then go to the GUI’s properties and check the active box for the button you are pressing.

script.Parent.Active = true

You need to create a plugin first:

-- Create a new toolbar section titled "Custom Script Tools"
local toolbar = plugin:CreateToolbar("Custom Script Tools")
 
-- Add a toolbar button named "Create Empty Script"
local newScriptButton = toolbar:CreateButton("Create Empty Script", "Create an empty script", "rbxassetid://4458901886")
 
local function onNewScriptButtonClicked()
	local newScript = Instance.new("Script")
	newScript.Source = ""
	newScript.Parent = game:GetService("ServerScriptService")
end
newScriptButton.Click:Connect(onNewScriptButtonClicked)

Source


You can always refer to the Developer Hub.

Thanks! Even though I see that you moved the script to where the widget should be created. Then changed the index of the button where it should be placed.