Code issues - help with mouse button click

Hello, how can I make sure that if I press the button all became → ‘true’ and if I press the same button again all became → ‘false’, can someone help me with that please!!

   local open = script.Parent.Parent.Dance

    open.MouseButton1Click:Connect(function()
    	script.Parent["Dance 1"].Visible = true
    	script.Parent["Dance 2"].Visible = true
    	script.Parent["Dance 3"].Visible = true
    	script.Parent["Dance 4"].Visible = true
    	script.Parent["Dance 5"].Visible = true
    	script.Parent["Dance 6"].Visible = true
    	script.Parent["Dance 7"].Visible = true
    	
    end)

Example, press the button (dance 1,2,3,4,5,6 and 7 became true) and then if i press same button again all (1,2,3,4,5,6 and 7) became false, how i do that?

Since you’re going 1-7, you can just do a loop. And change the bool value by setting it to it’s opposite value.

open.MouseButton1Click:Connect(function()
    for i = 1, 7 do
        script.Parent["Dance "..i].Visible = not script.Parent["Dance "..i].Visible
    end
end)
1 Like

Try that!

local open = script.Parent.Parent.Dance
local value = false
    open.MouseButton1Click:Connect(function()
    	script.Parent["Dance 1"].Visible = not value
    	script.Parent["Dance 2"].Visible = not value
    	script.Parent["Dance 3"].Visible = not value
    	script.Parent["Dance 4"].Visible = not value
    	script.Parent["Dance 5"].Visible = not value
    	script.Parent["Dance 6"].Visible = not value
    	script.Parent["Dance 7"].Visible = not value
    	value = not value
    end)
1 Like

I only need to make a menu, that when I press ‘dance’ all the buttons are shown and when I press dance again all the buttons are closed and thus, having ‘dance’ always visible

If I press the button again, how can I make it visible and then press again and it becomes invisible and so on?

Try the code I sent you, it is going to switch the value every time you click it.

1 Like

Works! thanks you !! :smiley: :smiley: :smiley:

1 Like

By the way, you may want to use the MouseButton1Down event since, sometimes, MouseButton1Click does not work with mobile devices.

You could also shorten your code a bit using for loops.

local open = script.Parent.Parent.Dance
local isOpen = false
open.MouseButton1Down:Connect(function()
    isOpen = not isOpen
    for danceNumber = 1, 7 do
       local danceObject = script.Parent:FindFirstChild("Dance " .. i)
       if not danceObject then
          continue
       end
       
       danceObject.Visible = isOpen
    end
end)