Clicking The Switch Again

  1. What do you want to achieve?
    I want it so if you click the button again, it will turn back to it’s current state.
  2. What is the issue?
    I don’t know any functions that do such thing.
  3. What solutions have you tried so far?
    Nothing Really.
local CollectionService = game:GetService("CollectionService")

for i, switch in CollectionService:GetTagged("Switch 1")  do
	local ClickDectect = Instance.new("ClickDetector")
	ClickDectect.Parent = switch
	
	
	ClickDectect.MouseClick:Connect(function()
		switch.Color = Color3.fromRGB(91, 154, 76)
	end)
end

That’s my script above…
How can I rechange it so when you click it again, the color of the switch changes?

For clarification do you want it to change back to its original color, or to a different, random color.

To it’s original color
and you can tell how to with a different color

and you can tell how to with a different color

What do you mean by this?

Like if you want too ig, just get to the point.

ok.

I was thinking that you could insert a color3 value into the switch to save it’s previous color, and then access it later to switch the color back to it’s original color, you would also need a bool value to know what color the switch is currently.

For ensample:

local CollectionService = game:GetService("CollectionService")

for i, switch in CollectionService:GetTagged("Switch 1")  do
	local ClickDectect = Instance.new("ClickDetector")
	ClickDectect.Parent = switch
	
	--looks for the values
	local switchedcolor = switch:FindFirstChild("BoolValue")
	local originalcolor = switch:FindFirstChild("ColorValue")
	
	if switchedcolor == nil or originalcolor == nil then
		--if no values exists, creates the values
		local switchedcolor = Instance.new("BoolValue",switch)
		switchedcolor.Name = "BoolValue"
		switchedcolor.Value = false
		local originalcolor = Instance.new("Color3Value",switch)
		originalcolor.Name = "ColorValue"
		originalcolor.Value = switch.Color
	end

	ClickDectect.MouseClick:Connect(function()
		if switchedcolor == false then
			--sets switch to desired color
			switch.Color = Color3.fromRGB(91, 154, 76)
			switchedcolor = true
		else
			--sets switch to original color
			switch.Color = originalcolor.Value
			switchedcolor = false
		end
	end)
end

Just so you know I never tested this so let me know if it doesn’t work. Also, this isn’t the best way to do this, a more optimized way would be to use OOP and metatables to do this but that would take too long for me to do.

It works! Thank you very much! it’s very helpful…

wow. that was fast. surprising.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.