Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make toggleable screengui button?

Asked by 4 years ago

I want to make a toggleable screengui button. Once I click the textbutton, the output should print "Toggled on." If I click it again the output should print "Toggled Off." I made a script, but it does not work.

function leftClick()
    print("Toggled on")
end
function LeftClickagain()
    print("Toggled off")
end

script.Parent.MouseButton1Up:Connect(LeftClickagain)
script.Parent.MouseButton1Down:Connect(leftClick)
1
all the answers suck, use TweenPosition like a chad greatneil80 2647 — 4y
0
Dude. He doesn't know what MouseButton1Down and MouseButton1Up is. How is he going to figure out how to tween things? Unhumanly 152 — 4y
0
And my answer is adequate at the very least :^[ Unhumanly 152 — 4y
0
You all got wooshed Psudar 882 — 4y

3 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Alright, So ill make a toggle valuable, When it toggles to true it prints off and if on and clicked it toggles off here is a script that worked with me.

local Toggle = true

script.Parent.MouseButton1Click:Connect(function()
    if Toggle == true then
        print("Toggle on")
        Toggle = false
    elseif Toggle == false then
        print("Toggle off")
        Toggle = true
    end
end)

Explanation:

There so when toggle is on and the player clicks, It checks if the toggle is on or off it is on then says Toggle on else if it's not it says toggle off.

Goodbye.

0
Thank you so much! GamingWithFlight 80 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

So, when you click the button, it toggles on. But, your issue is that it toggles off when you lift your mouse button. You can easily fix this by creating a variable.

local varToggle = false -- The variable to handle the toggles.
local btnToggle = script.Parent -- The button to toggle.

function btnToggleClicked()
    varToggle = not varToggle
    print("Toggle : ".. tostring(varToggle))
end
btnToggle.MouseButton1Click:Connect(btnToggleClicked)

Hopefully, this was helpful and useful to you.

Goodbye, ItzPartapika

Log in to vote
0
Answered by
Unhumanly 152
4 years ago
Edited 4 years ago

I know answers have already been provided, but I want to go into full detail.

Your goal is to make a button that is toggleable by clicking it, meaning you press your left mouse button down, and release it, which would be considered a click.

For that, you would use the MouseButton1Click event.

MouseButton1Down is an event that fires when the left mouse button is pressed down.

MouseButton1Up is an event that fires when the left mouse button is released, after being pressed down.

MouseButton1Click is a combination of these two events, that fires after the left mouse button is pressed down, and then is released.

Your script would print both of the print statements in a single click. Your script is toggling the button to on when you press the left mouse button down, and to off when you release the left mouse button.

I realize that iiClearlyDeveloper has already provided a corrected version, but this is how I would personally do it.

local Toggle = "off"

script.Parent.MouseButton1Click:Connect(function()
    if Toggle == "off" then
        Toggle = "on"
        print(Toggle)
    else
        Toggle = "off"
        print(Toggle)
    end
end)

What this does is creates a variable, and sets it to the string "off". This will allow us to determine the current state of the button.

When the button has had the left mouse button pressed down on it, and has had the left mouse button released while it is still hovering over the button, the MouseButton1Click event will fire, prompting the code inside of the event listener to run.

The if statement will change the state of the button, based on its current state.

If the button is toggled off, then toggle the button on. If it is not toggled off, then it surely is toggled on, so toggle it off.

I just wanted to break down all of this for you, so you understand exactly what's happening. I don't want to throw some code at you and not have you know what I've done.

Answer this question