Different action for hitting a click detector a 2nd time

Hello, I am kevman599! About a few days ago I started learning how to code, robloxes api reference is a big help to learn, but anyway, in this process, I am making a tv, and a button to turn it on and off, except I don’t know how to make it do a different action like turning it back off or on, on a second click, any tutorials or ways I can make a different action happen on a second click?

put this as the click detector function (you don’t need to replace, you can just implement it into yours):

local clicks = 0
clickdetector.MouseClick:Connect(function()
clicks += 1
if clicks == 1 then
--do script
elseif clicks == 2 then
clicks = 0
-- do script
end
end)
2 Likes

I will put some research into this. It seems really helpful.

this is way easier,
basically it sets the value to the opposite so if its true > false, if false > true.

local isTvOn = false
local clickDetector = script.Parent.ClickDetector

function TVButtonClicked()
	isTvOn = not IsTvOn
end

clickDetector.MouseClick:Connect(TVButtonClicked)
2 Likes

So I am stupid and was using a clickdetector earlier,it is a imagebutton i’m using, what would this change?

you’re not stupid. your code would still look like this:

   if isTvOn  then
		--TODO: turn tv off
		isTvOn = false
		
	else
		--TODO: turn tv on
		isTvOn = true  
	end

just inside a mousebutton1click event instead of MouseClick event.

isTvOn = not IsTvOn is another way of changing isTvOn to true/false, but it does it in one line instead of two.

how it works is if isTvOn is true it sets isTvOn to not true and not true means false. vice versa for if isTvOn was false.

you can write it either way, but maybe stick to isTvOn = true / isTvOn = false for now since you’re just learning it.

2 Likes

Thank you so much. You were very helpful.

1 Like

if you for example, want to turn the screen on as well, you should use.

if isTvOn then
aka royaltoe’s method