How would i make a system like this?

What i want to do is create a way to close the menu up once its opened by just clicking the button you clicked?
Ive already tried several ways but none of them work like destroying it if it finds the menu in the local players GUI

local Button = script.Parent
local CraftingMenu = game.ReplicatedStorage.CraftingMenu

Button.MouseButton1Click:Connect(function(Clicked)
	local CraftingMenuClone = CraftingMenu:Clone()
	CraftingMenuClone.Parent = game.Players.LocalPlayer.PlayerGui
end)

Could you explain your problem better? I didn’t understand

Are you saying you want a toggle button? That the same button opens and closes it?

So what i want is a system where if you click the crafting button you well open the menu but you can click the crafting button again this time close the crafting menu

If you want the button to act both as an open-close button, you can add a condition to it. Say:

local menuopen = false
Button.MouseButton1Down:Connect(function(Clicked)
      if not menuopen then 
      menuopen = true
      --- open the menu
      else
      menuopen = false
      -- close the menu
end)

When i use this and put the script in that opens and close it closes for .5 seconds and then just reopens

local Button = script.Parent
local CraftingMenu = game.ReplicatedStorage.CraftingMenu

local menuopen = false
Button.MouseButton1Down:Connect(function(Clicked)
	if not menuopen then
		menuopen = true
		Button.MouseButton1Click:Connect(function(Clicked)
			local CraftingMenuClone = CraftingMenu:Clone()
			CraftingMenuClone.Parent = game.Players.LocalPlayer.PlayerGui
		end)
	else
		menuopen = false
		game.Players.LocalPlayer.PlayerGui.CraftingMenu:Destroy()
	end
end)

and yeah thats what i want out of the script

thats what i want out of of the script im trying to make

local Button = script.Parent
local CraftingMenu = game.ReplicatedStorage.CraftingMenu

local menuopen = false
local CraftingMenuClone = nil

Button.MouseButton1Down:Connect(function()
    if not menuopen then
        menuopen = true
        CraftingMenuClone = CraftingMenu:Clone()
        CraftingMenuClone.Parent = game.Players.LocalPlayer.PlayerGui
    else
        menuopen = false
        if CraftingMenuClone then
            CraftingMenuClone:Destroy()
            CraftingMenuClone = nil
        end
    end
end)

1 Like

you were connecting the MouseButton1Click event inside the MouseButton1Down event, which is likely causing the menu to reopen immediately after closing.

1 Like

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