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

How To Make a Script Where You Click Once It Does Something, But When You Click The Second Time...?

Asked by 8 years ago

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!

0
What exactly are you trying to accomplish? A double-click method of some sort? dyler3 1510 — 8y

3 answers

Log in to vote
0
Answered by 8 years ago

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)
0
Thank you so much for the answer, but what if you click the third time? GatitosMansion 187 — 8y
0
Well, i suppose you could make a number value, and use if statements to execute code based off what number of times you clicked it. CodingEvolution 490 — 8y
0
Thank you for your response! However, I am not a good scripter, so can you give me an example please? Thanks! GatitosMansion 187 — 8y
0
Sure. I'll update the answer. CodingEvolution 490 — 8y
View all comments (2 more)
0
Thank you so much! I would give you reputation, but I can't since I need at least 5 GatitosMansion 187 — 8y
0
No problem, lol CodingEvolution 490 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

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)
Log in to vote
0
Answered by
TopDev 0
8 years ago
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)

0
Thank you so much for the answer, but what if you click the third time? GatitosMansion 187 — 8y

Answer this question