How do I make a GUI toggle button?

How do I make a script go from

image
to
image
then back to
image
then continue so on.

Like a toggle button.

Here is the script:

script.Parent.MouseButton1Click:Connect(function()
	script.Parent.Parent.FrameA.BackgroundTransparency = 1
	script.Parent.Parent.FrameB.BackgroundTransparency = 0
end)

Here is the form:

image

3 Likes

You can use a variable for this:

local isOpen = false 

--click event
if not isOpen then
    --not toggled, so toggle the UI
else
     --toggled, so untoggle the UI
end

isOpen = not isOpen --flip the value of the bool
4 Likes

When using the script.Parent.MouseButton1Click:Connect(function()
there is an error on “isOpen” on a local script.


--click event
if not isOpen then
	script.Parent.Parent.FrameA.BackgroundTransparency = 0
	script.Parent.Parent.FrameB.BackgroundTransparency = 1
	--not toggled, so toggle the UI
else
	script.Parent.MouseButton1Click:Connect(function()
	script.Parent.Parent.FrameB.BackgroundTransparency = 1
	script.Parent.Parent.FrameA.BackgroundTransparency = 0
end

isOpen = not isOpen

I may be forming it incorrectly as I’m not the best with scripting.

So, I’ll explain this logically so you understand what is going on here, what you’re looking to do is to check if the button is toggled by using a bool i.e:

local Toggled = false

if Toggled == false then
     Toggled = true
elseif Toggled == true then
      Toggled = false
end

This will check if it is open or not, and set it accordingly. I will not give you the code DIRECTLY, so I’ll have you figure it out. So, what you want to do next is change the color of red to green, this can be done via this:

Frame.Color3 = Color3.fromRGB(0,255,0)

and then you want to set it’s position to the right, simply you can just add on the position to the frame that exists, which I’m assuming it does, and assuming it’s the red frame.

You do this:

 Frame:TweenPosition(Frame.Position + UDim2.new(.5,0,0,0),"In","Sine",.1) -- You can change it to whatever you like.

Then to do it to un-toggle, you do it backwards. Good luck!

8 Likes

You put the toggle checking statement inside of mousebutton1click function, and inside of the toggle statement you either turn it on or off, you’ll figure it out in time! Good luck!

2 Likes