What does PluginToolbarButton:SetActive() do?

Hello,

I am currently trying to create a plugin. I’m very new to plugin creation (I am touching it for the first time today). I’ve created a PluginToolbar, and then a PluginToolbarButton. I have built a functionality to happen when I click the button. I was then, in testing, attempting to edit the functionality of the button to make it immediately leave the “pressed” state. This isn’t necessary, but for the sake of looks I wanted it to be pressed, and then immediately set itself to the unpressed state.

In looking through the DevHub, I have found the function :SetActive(), which is a method of PluginToolbarButton. On the page it seemed as though this function could set the visual state of the button. It doesn’t do that…
Here is my script:

--Creating the toolbar/buttons
local toolbar = plugin:CreateToolbar("Test")
local button = toolbar:CreateButton("OpenUi", "Opens the UI", "rbxassetid://9081569014","OPEN UI")
button.Enabled = true
button.ClickableWhenViewportHidden = true

--Creating the Widget
local DockInfo = DockWidgetPluginGuiInfo.new(Enum.InitialDockState.Float, false, false, 200, 200, 100, 100)
local Widget = nil

local function createWidget()
	Widget = plugin:CreateDockWidgetPluginGui("TestUi", DockInfo)
	Widget.Title = "Test"
	Widget.ResetOnSpawn = false
end

local function onButtonClicked()
	print("Button clicked")
	if not Widget then
		createWidget()
	end
	button:SetActive(false)
	Widget.Enabled = not Widget.Enabled
end
button.Click:Connect(onButtonClicked)

Is there a different function I can use to achieve this? I’ve seen this kind of behavior before, so I’m inclined to believe it’s possible.

Thanks in advance for the help!

I have come across a similar issue, a button that is a single-use action (i.e. not a toggle) that remains as “pressed” after use. There is no proper (that I’m aware of) function for changing button state but you can reset it by wrapping a .Enabled state change around the code. This also acts as a debounce to stop people running the code again before the first run has finished:

local function onButtonClicked()
	button.Enabled = false
	print("Button clicked")
	if not Widget then
		createWidget()
	end
	button:SetActive(false)
	Widget.Enabled = not Widget.Enabled
	button.Enabled = true
end

For opening/closing a widget, I would suggest not doing this since the button state matches (or should do, it can be a bit glitchy with the script editor) the widget state.

Edit, instead of a reply to prevent spam: That’s a nice way of doing it, I hadn’t thought of that. It also means that if they somehow get decoupled then closing the widget syncs them again.

1 Like

Thank you!
Taking into account what you said with the button state aligning with the UI, I realize that that is important, and so I have changed the code a bit. Now, I have edited the createWidget() function:

local function createWidget()
	Widget = plugin:CreateDockWidgetPluginGui("FurnitureCreationUi", DockInfo)
	Widget.Title = "Create Furniture"
	Widget.ResetOnSpawn = false
	Widget:BindToClose(function()
		button.Enabled = false
		Widget.Enabled = false
		button.Enabled = true
	end)
end

Now whenever the widget is closed, the button will reflect the widget state.

1 Like