Help with Switch On and Off

Hello my name is Thunder
and this is my first post on Scripting Support

I have a problem:
See, I am making a Settings GUI and I’ve made a switch for the settings to turn it on and off

image
But, here’s the problem:
I’ve made a script on it to make it switch from OFF to ON but I think I’ve made it one way which I don’t want it to be.
Here is the script:

local follow = script.Parent.Parent.Frame

script.Parent.MouseButton1Click:Connect(function()
	wait(0.5)
	
    script.Parent:TweenPosition(UDim2.new(0.644, 0,-0.017, 0),"Out", "Bounce", 0.5, true)

	follow:TweenSize(UDim2.new(0, 60,0, 19),"Out", "Bounce", 0.5, true)
	
end)

And when you test it:
image
It works, YAY but I don’t know how to make a script to turn it back off.

I hope someone can help
Thanks,
Thunder.

1 Like

A simple thing to do is to define a bool at the start and run a check in your function to detect whether to turn the setting on or off.

Example code:

local isOn = false

script.Parent.MouseButton1Click:Connect(function()
    if isOn then
        —Turn the setting off
    elseif not isOn then
        — Turn setting on
    end
end)
4 Likes

Thank you :slight_smile: , I will try it

Thunder

You basically need a boolean(or anything similar to note 2 states of the button) and the two different positions per state. It appears you are using constant values. Incremental methods should do the trick as well.

2 Likes

Oh I understand it better now

Thank you

Have a boolean that you set it to true when the thing is activated and set it to false when the thing is deactivated.

Then you do a conditional and check if the player clicked when it was deactivated. If that actually happened then you do that script you already have.
If it was activated then you do a tween so it goes back to the original position (You can save it before doing the script by placing a variable before the event)

Basically it would do something like this:

local originalPos = (The position of the GUI)
local activated = false
script.Parent.MouseButton1Click:Connect(function()
         if activated == false then
            --Switch On
	        wait(0.5)
	
            script.Parent:TweenPosition(UDim2.new(0.644, 0,-0.017, 0),"Out", "Bounce", 0.5, true)

	        follow:TweenSize(UDim2.new(0, 60,0, 19),"Out", "Bounce", 0.5, true)

         elseif activated == true then
             --Switch Off

         end
end)
1 Like

Thanks, guys for helping me out a lot, I have scripting experience but I guess I forgot about it, thanks a lot for refreshing my memory :slightly_smiling_face:

1 Like