Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How does one make this Open/Close via Button Toggle?

local Player = game:GetService("Players").LocalPlayer
local GuiParent = game:GetService("CoreGui")
local Gui = GuiParent:FindFirstChild("RRADIO") or Instance.new("ScreenGui", game.CoreGui)
Gui.Name = "RRADIO"
Gui:ClearAllChildren()

local GuiOpen = false

--MainFrame.
local MainFrame           = Instance.new("Frame", Gui)
MainFrame.Size            = UDim2.new(0, 400, 0, 155)
MainFrame.Draggable       = true
MainFrame.Active          = true
MainFrame.BackgroundColor = BrickColor.new("196")
MainFrame.Position        = UDim2.new(1, -1360, 1, -155)
MainFrame.BorderSizePixel = 2
MainFrame.Style           = "Custom"

--Sound TextBox.
local ArgumentBox = Instance.new("TextBox", MainFrame)
ArgumentBox.Size = UDim2.new(0, 235, 0, 35)
ArgumentBox.Position = UDim2.new(0, 15, 1, -143)
ArgumentBox.BorderSizePixel = 3
ArgumentBox.BackgroundColor = BrickColor.new("27")
ArgumentBox.Text = "Paste Audio ID Here."
ArgumentBox.Font = "Arial"
ArgumentBox.FontSize = 6
ArgumentBox.TextColor = BrickColor.new("212")

--Play Button.
local Button = Instance.new("TextButton", MainFrame)
Button.Size = UDim2.new(0, 100, 0, 35)
Button.Position = UDim2.new(0, 272, 0, 12)
Button.Text = "Play"
Button.BorderSizePixel = 2
Button.BackgroundColor = BrickColor.new("213")
Button.TextColor = BrickColor.new("212")
Button.Font = "Arial"
Button.FontSize = 6

--Play Button Function.
Button.MouseButton1Click:connect(function()
local Plrs = ArgumentBox.Text
local s = Instance.new("Sound", game:GetService("Workspace"))
s.Name = "SONGNAME"
s.SoundId = 'rbxassetid://' .. Plrs
s.Volume = 10
s.Looped = true
s.archivable = false
wait(0)
s:Play()
end)

--Stop Button.
local Stop = Instance.new("TextButton", MainFrame)
Stop.Size = UDim2.new(0, 100, 0, 35)
Stop.Position = UDim2.new(0, 272, 0, 60)
Stop.Text = "Stop"
Stop.BorderSizePixel = 2
Stop.BackgroundColor = BrickColor.new("213")
Stop.TextColor = BrickColor.new("212")
Stop.Font = "Arial"
Stop.FontSize = 6

--Stop Button Function.
Stop.MouseButton1Click:connect(function()
game:GetService("Workspace").SONGNAME:Remove()
end)

--Volume Textbox.
local VolumeBox = Instance.new("TextBox", MainFrame)
VolumeBox.Size = UDim2.new(0, 60, 0, 35)
VolumeBox.Position = UDim2.new(0, 15, 1, -95)
VolumeBox.BorderSizePixel = 2
VolumeBox.BackgroundColor = BrickColor.new("213")
VolumeBox.Font = "Arial"
VolumeBox.Text = "Vol"
VolumeBox.FontSize = 5
VolumeBox.TextColor = BrickColor.new("212")

--Volume Button.
local VolumeButton = Instance.new("TextButton", MainFrame)
VolumeButton.Size = UDim2.new(0, 30, 0, 35)
VolumeButton.Position = UDim2.new(0, 95, 0, 60)
VolumeButton.Text = "Set"
VolumeButton.BorderSizePixel = 2
VolumeButton.BackgroundColor = BrickColor.new("213")
VolumeButton.TextColor = BrickColor.new("212")
VolumeButton.Font = "Arial"
VolumeButton.FontSize = 5

--Open/Close Toggle Button
local OpenCloseButton = Instance.new("TextButton", MainFrame)
OpenCloseButton.Size = UDim2.new(0, 30, 0, 35)
OpenCloseButton.Position = UDim2.new(0, 95, 0, -50)
OpenCloseButton.Text = "ToggleGui"
OpenCloseButton.BorderSizePixel = 2
OpenCloseButton.BackgroundColor = BrickColor.new("213")
OpenCloseButton.TextColor = BrickColor.new("212")
OpenCloseButton.Font = "Arial"
OpenCloseButton.FontSize = 5

--Loggo.
local Loggo = Instance.new("TextButton", MainFrame)
Loggo.Size = UDim2.new(0.82, 30, 0, 35)
Loggo.Position = UDim2.new(0, 15, 0, 110)
Loggo.Text = "Edited by: [HolyNEvil62]"
Loggo.BorderSizePixel = 2
Loggo.BackgroundColor = BrickColor.new("213")
Loggo.TextColor = BrickColor.new("212")
Loggo.Font = "Arial"
Loggo.FontSize = 10

--Volume Button Function.
VolumeButton.MouseButton1Click:connect(function()
local Plrs = VolumeBox.Text
local s = game:GetService("Workspace").SONGNAME
s.Volume = Plrs
end)

2 answers

✓ Accepted answer

Normally, I would do this:

local Button = script.Parent 

Button.MouseButton1Click:Connect(function()
	if Button.Visible == false then
		Button.Visible = true
	else -- if the button is visible
		Button.Visible = false
	end
end)

But then I learned a brilliant timesaver from AlvinBlox when you want to script an open/close button.

local Button = script.Parent 

Button.MouseButton1Click:Connect(function()
	Button.Visible = not Button.Visible
end)

Basically, not is an inverter. not true is the same thing as false, and not false is the same thing as true. It doesn't matter what the value of Visible is. It can be either true or false. not will turn the value into the opposite of what it is. You don't need to check if the Button is visible or not visible. not will just invert it for you.

--First add a variable at the top named open
local open = true

--Code
OpenCloseButton.MouseButton1Click:Connect(function()
	if open then
		open = false
		MainFrame.Visible = false
	else
		open = true
		MainFrame.Visible = true
	end
end)

-Ducky

Developer

Youtuber

This discussion is preserved as it was. New answers, comments, and votes are closed.