Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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!

1 comment

0

What exactly are you trying to accomplish? A double-click method of some sort?

3 answers

✓ 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 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)

6 comments

0

Thank you so much for the answer, but what if you click the third time?

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.

0

Thank you for your response! However, I am not a good scripter, so can you give me an example please? Thanks!

0

Sure. I'll update the answer.

0

Thank you so much! I would give you reputation, but I can't since I need at least 5

0

No problem, lol

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)

1 comment

0

Thank you so much for the answer, but what if you click the third time?

This discussion is preserved as it was. New answers, comments, and votes are closed.