Issues making an open/close button for a UI

I’m currently working on a music UI, but I’m having issues on scripting an open close system. I want the script to check if the playlist is opened, and if its opened, it closes the ui. But the problem is, when i open, it closes automatically. I’m pretty bad at scripting, so it can be a small issue as well which I couldn’t find.

Parents

image

Script (PlaylistOpenHandler)

--//Techyfied\\--

local playlistFrame = script.Parent.Playlist
local playlistButton = script.Parent.MainFrame.PlaylistOpen
local closeButton = script.Parent.Playlist.Close

local isOpened = false


closeButton.MouseButton1Click:Connect(function()
	if isOpened == false then
		print("Could not be closed because it's already closed. How did you even click this button lol")
	end
	
	if isOpened == true then
		print("Playlist is opened. Closing..")
		playlistFrame:TweenPosition(UDim2.new(-0.3, 0, 0.227, 0), 'Out', 'Back', 1)
		isOpened = false
		print("Successfully closed.")
		wait(1)
	end
end)

playlistButton.MouseButton1Click:Connect(function()
	if isOpened == false then
		print("Play list is closed. Opening..")
		playlistFrame:TweenPosition(UDim2.new(0.023, 0, 0.227, 0), 'Out', 'Back', 1)
		isOpened = true
		print("Successfully opened.")
		wait(1)
	end
	
	if isOpened == true then
		print("Playlist is opened. Closing..")
		playlistFrame:TweenPosition(UDim2.new(-0.3, 0, 0.227, 0), 'Out', 'Back', 1)
		isOpened = false
		print("Successfully closed.")
	end
end)

Output after clicking on the PlaylistOpen button only

image

Here, UDim2.new(-0.3, 0, 0.227, 0) is the Playlist frame’s closed position and UDim2.new(0.023, 0, 0.227, 0) is the Playlist frame’s opened position. The frame appears, waits 1 second and it closed. And that’s my issue. Any help is appreciated! :heart:

You’re setting isOpened to true when the if statement it is in is before the one that checks that it’s true. Try to use else statements or guard clauses.

Example of why it’s wrong, as you can see if isOpened becomes false, the second if statement will run even though both are checking the boolean statement oppositely.

if isOpened then
    isOpened = false
end

if not isOpened then
    isOpened = true
end

Guard clauses solution:

if isOpened then
    isOpened = false
    return -- stops the function from
running further
end

if not isOpened then
    isOpened = true
end

I used else. And it worked. Thanks for the help!

Here’s my new script:

local playlistFrame = script.Parent.Playlist
local playlistButton = script.Parent.MainFrame.PlaylistOpen
local closeButton = script.Parent.Playlist.Close

local isOpened = false


closeButton.MouseButton1Click:Connect(function()
	if isOpened == false then
		print("Could not be closed because it's already closed. How did you even click this button lol")
	
	else
		print("Playlist is opened. Closing..")
		playlistFrame:TweenPosition(UDim2.new(-0.3, 0, 0.227, 0), 'Out', 'Back', 1)
		isOpened = false
		print("Successfully closed.")
		wait(1)
	end
end)

playlistButton.MouseButton1Click:Connect(function()
	if isOpened == false then
		print("Play list is closed. Opening..")
		playlistFrame:TweenPosition(UDim2.new(0.023, 0, 0.227, 0), 'Out', 'Back', 1)
		isOpened = true
		print("Successfully opened.")
		wait(1)
		
	else
		print("Playlist is opened. Closing..")
		playlistFrame:TweenPosition(UDim2.new(-0.3, 0, 0.227, 0), 'Out', 'Back', 1)
		isOpened = false
		print("Successfully closed.")
	end
end)
1 Like