Plugin. Start function on plugin un-clicked

Hola, I’m making a plugin,
I have put a function that starts when the plugin is clicked, but i would like to add a function that starts when the plugin is clicked again but to de-activate it.
Pratically the opposite of the .Click one.

This is my code

--Configuro il plugin
local toolbar = plugin:CreateToolbar("Settaggio delle posizioni")
local pluginButton = toolbar:CreateButton(
	"Metti Script", --Cosa appare sotto il plugin
	"Bottone che serve a inniettare lo script necessario", --Testo visibile solo se si è sopra
	"rbxassetid://11497000075" --Icona
) 


--Variabili
local Giocatore
local Gui


--Se il plugin è cliccato
pluginButton.Click:Connect(function()
	print("Inizio settaggio")
	
	--Metto il Gui al giocatore
	Gui = script.Parent.Gui["Settaggio Posizioni"]:Clone()
	Gui.Parent = game.StarterGui
end)

I tried serching around some commands, but nothing that i’m aiming to appears. Thanks

You should NOT be using StarterGui for plugin GUIs, instead you should use DockWidgetPluginGui’s, here is a simple tutorial in making plugins:

Why shouldn’t I use startergui?

Because it will be in the game when you play test it? It’s better to use DockWidgetPluginGui’s because you wouldn’t need to perform a useless check that will destroy the GUI if they are playtesting.

oh but it’s ok, cause i made a plugin that i will only use for myself. So i can just delete the folder with all the things when i want.

Okay then, you can just check if it already exists by doing :FindFirstChild() on StarterGui.

pluginButton.Click:Connect(function()
	print("Inizio settaggio")
	
    if game.StarterGui:FindFirstChild('Settaggio Posizioni') then
        game.StarterGui['Settaggio Posizioni']:Destroy()
    else
    	Gui = script.Parent.Gui["Settaggio Posizioni"]:Clone()
	    Gui.Parent = game.StarterGui
    end
end)

Edit: Nevermind, just realized that you stored a variable containing the GUI instance, you can actually just do this instead:

pluginButton.Click:Connect(function()
	print("Inizio settaggio")
	
    if Gui then
        Gui:Destroy()
        Gui = nil
    else
    	Gui = script.Parent.Gui["Settaggio Posizioni"]:Clone()
	    Gui.Parent = game.StarterGui
    end
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.