Issue with mouse event

I made a plugin locally that has a custom GUI when I select an object it shows which object is selected and if it has code associated with it. I can then edit the code or insert code if none is present. The plugin button shows and hides the GUI. I have all this working but if I load up a project to test the plugin the buttons on the GUI don’t work. It will show which object is selected and if it has code also show the appropriate button (insert or edit).

If I load the original project it still doesn’t work, until I save the plugin again locally then everything is working as it should.

I tried moving the code around but that just breaks stuff, I tried using user input service instead of mousebutton but that does nothing.

I feel like I am just missing something when it comes to plugins but I can’t seem to figure it out. Any ideas?

It would be really useful if you could provide more than just a paragraph, whilst being a detailed one, showing screen shots of error(s) and or inserting code into your post that is erroring would be really helpful for us to be able to help you. Also, do you move the code to CoreGui? What other method(s) have you tried if any, and what have you considered?

Capture
This is the only error I get when studio loads with the plugin since it want to load the results of the selection but the window hasn’t been loaded. I have been ignoring it at the moment.

Capture2
This is how the plugin is organized at the moment.

local guiLocation = game.CoreGui
	
local toolbar = plugin:CreateToolbar("Custom Script Tools") -- Create a new toolbar section titled "Custom Script Tools"
local activateClickManager = toolbar:CreateButton("Manage Mouse Clicks", "Easily manage clicks", "rbxassetid://113701277")-- Add a toolbar button

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------	
local function onActivateMangerClicked()
		--Toggle GUI
		local createGUI = game:GetService("CoreGui"):FindFirstChild("ClickerWindow")
		
		if not createGUI then
			createGUI = script.ClickerWindow:Clone()
			createGUI.Parent = guiLocation
		else
			createGUI:Destroy()
		end
end
	
	-----------------------------------------------------------------------------------------------------------------------------------------------------------------	
	function findClickEvent(selectionName)
		local scriptLocation = nil
		local guiShortcut = guiLocation.ClickerWindow.Frame
		selectionName = selectionName:gsub("%s+","_") --no spaces in names
		
		if scriptLocation then
			--find script code
				guiShortcut.functionName.Text = selectionName
				guiShortcut.TextButton.Visible = false
				guiShortcut.EditButton.Visible = true
			else
				--print("Action not found")
				guiShortcut.functionName.Text = selectionName
				guiShortcut.TextButton.Visible = true
				guiShortcut.EditButton.Visible = false
	
				--clear window text
				guiShortcut.ScrollingFrame.functionContents.Text = ""
			end
	end

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
	local selection = game:GetService("Selection")
	selection.SelectionChanged:Connect(function()
		guiLocation.ClickerWindow.Frame.statusbar.Text = "Nothing Selected"
			--permit only one selection
		if selection:Get() then
			for _, object in pairs(selection:Get()) do
				findClickEvent(object.Name)
				guiLocation.ClickerWindow.Frame.statusbar.Text = object.Name .. " Selected"
			end
		end
	end)
	

activateClickManager.Click:Connect(onActivateMangerClicked)

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

function insertClickEvent()
	print("make an edit event")
end
	
editButtonActivation = guiLocation.ClickerWindow.Frame.TextButton.MouseButton1Click:Connect(insertClickEvent)

That is the basic code that replicates the issue, still some extra stuff in there but it should be understandable.

I clone the GUI to CoreGui but not the script, after reading that I tried including the button click code under clicker window but still got the same issue. Anything after activateClickManager.Click:Connect(onActivateMangerClicked) does not work when I start a new project. As soon as I “save the code as local plugin” it works fine. Yet I can’t include the button click data before the activate because then the activate does not work.

Feels like I need a way to load that code in but can’t figure out how.

Figured it out!!

I put the event in a separate script and localscript because I read it need to be in a local but that didn’t fix it.

I remembered you asking about errors and thought it may be the error I was getting that was the problem, it must be stopping the click event from running because it could not find the GUI. I just could not figure out how to test it since I can’t have it wait for the GUI because I can’t be sure it will get turned on. I tried disabling the scripts than enabling but that just makes it not run at all. Then I remembered I saw CoreGui had a ChildAdded event.

I still get the error (probably because I didn’t add the wait the code says) but now it works! I put the code in a separate LocalScript might not be necessary but I already had it set up that way from testing.

local guiLocation = game.CoreGui

game.CoreGui.ChildAdded:Connect(function(child)
	-- need to use WaitForChild as descendants may not have replicated yet
	--local head = child:WaitForChild("Head")
	guiLocation.ClickerWindow.Frame.TextButton.MouseButton1Click:Connect(insertClickEvent)
end)

function insertClickEvent()
	print("make an edit event")
end

The code is from the Roblox developer page.

Thanks for the help.