How To Make a Script Where You Click Once It Does Something, But When You Click The Second Time It Does Something Else? I tried this, but of course it doesn't work...
function onClicked() --Code function onClicked() --Code end end script.Parent.MouseButton1Click:connect(onClicked)
Thank you!
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 is true, then... if Bool then -- This will be what happens on the first click print('Bool is true') else -- If bool is false, this is what will happen on the second click print('Bool is false') end -- We can use the "not" operation to convert the bool to it's opposite. 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 is equal to 1, then... if Bool == 1 then -- This will be what happens on the first click print('Bool is '..Inc) elseif Bool == 2 then -- If bool is 2, then ... print('Bool is '..Inc) elseif Bool == 3 then -- If bool is 3 then ... print('Bool is '..Inc) end -- Now, let's add 1 to "Inc". 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 our index exists, then ... if ButtonEvents[Inc] then -- Call the value associated with the key (must be a function) ButtonEvents[Inc]() end -- Increase the increment Inc = Inc + 1 end)
Problem
I see your problem but you didn't really do anything besides ask so Il just give you a script
Solution
clicked = false script.Parent.MouseButton1Click:connect(function() if clicked == false then --1st click clicked = true end if clicked == true then --2nd click end end)
clicked = false script.Parent.MouseButton1Click:connect(function() if clicked == false then clicked = true -- do stuff elseif clicked == true then clicked = false -- do stuff end end)