Need help with plugin widget

Hello devforum,
Today I’m working on a new plugin, however, I’m having issue with the widget.
Here’s my code:

assert(plugin, "[FLARE]: Sorry, Sun Animator must be run as a plugin.")

-- Services
local ChangeHistoryService = game:GetService("ChangeHistoryService")
local Selection = game:GetService("Selection")

-- Requirements
local RequirementsModule = require(script.Requirements)

-- Plugin establishment
local toolbar = plugin:CreateToolbar("Sun Animator")
local gui = script.Parent.SunGate

-- Button Establishment
local OpenMenuButton = toolbar:CreateButton("Open Sun", "Open Sun GUI Animator", "rbxassetid://89220289952455")
OpenMenuButton.ClickableWhenViewportHidden = true -- Enables button in scripting windows
local WidgetInfo = DockWidgetPluginGuiInfo.new(
	Enum.InitialDockState.Float,
	true,
	true,
	1200,
	800,
	1200,
	800
) -- Window size requirements for Sun.

local widget
widget.Enabled = false

local function buttonpress()
	print("Opening Sun")
	if widget.Enabled == false then
	widget = plugin:CreateDockWidgetPluginGui("SunAnimatorWindow", WidgetInfo)
	widget.Enabled = true
	widget.Title = "Sun Animator"
	else
		widget.Enabled = true
	end
	local MF = gui:WaitForChild("Main"):Clone()
	MF.Parent = widget
	MF.Position = UDim2.new(0, 0, 0, 0)
	MF.Size = UDim2.new(1, 0, 1, 0)
	MF.Visible = true
end

local function closewidget()
	widget.Enabled = false
end

OpenMenuButton.Click:Connect(buttonpress())

widget:BindToClose(closewidget())

The documentation for plugins isnt very straightforward, and I’ve been trying to fix this now for about an hour or 2. The issue is that when I close the widget and click the button again, it won’t reopen and I don’t know how to fix this.
I’ve tried fixing it by toggling the enabled value on the widget however now when i press the button it just doesn’t appear at all.

If anyone can help, please reply.

just do this in the buttonPress function


local function buttonpress()
   widget.Enabled = not widget.Enabled -- Sets it to the opposite.

   if not widget.Enabled then
      -- stuff here
   end
end

also instead of cloning new UI every time the UI is open, just keep the same UI

Ok thats more of just optimizing your code, one question, what does this do?

Currently experimenting with your solution
That bindtoclose function was one of my failed attempts to solve the issue.

1 Like

Well probably remove it as it might mess with some stuff probably? Idk