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.
4 comments
all the answers suck, use TweenPosition like a chad
Dude. He doesn't know what MouseButton1Down and MouseButton1Up is. How is he going to figure out how to tween things?
And my answer is adequate at the very least :^[
You all got wooshed