✓ Accepted answer
I'd just be repeating other answers, but i'd like to explain why it works.
Toggling
We can create a variable with a boolean value, that changes the first time we click it, like this:
local Bool = true
local Button = script.Parent
Button.MouseButton1Click:connect(function()
if Bool then
print('Bool is true')
else
print('Bool is false')
end
Bool = not Bool
end)
Hope that made it a bit more clear, if not already.
EDIT:
(in response to your counter question)
If we wanted to have different actions happen more than twice, we could use a number value as an increment, instead of a bool as a binary statement. Like this:
local Inc = 1
local Button = script.Parent
Button.MouseButton1Click:connect(function()
if Bool == 1 then
print('Bool is '..Inc)
elseif Bool == 2 then
print('Bool is '..Inc)
elseif Bool == 3 then
print('Bool is '..Inc)
end
Inc = Inc + 1
end)
You could even make an array of events that we can index with the "Inc" variable. Like this:
local Button = script.Parent
local Inc = 1
local ButtonEvents = {
function()
print('Event 1')
end,
function()
print('Event 2')
end,
function()
print('Event 3')
end
}
Button.MouseButton1Click:connect(function()
if ButtonEvents[Inc] then
ButtonEvents[Inc]()
end
Inc = Inc + 1
end)
1 comment
What exactly are you trying to accomplish? A double-click method of some sort?