Scripting Click Detector event help

Hi guys. Im a builder, trying to learn scripting but I’m having one issue with the click detector function. I’m trying to make one large canvas (Seperate blocks) where, when you click the block, it turns black, but you can also click it again to make it return back to it’s original colour, which in my case, is ‘Fossil’

script is:

part = script.Parent

function OnClick()
part.BrickColor = BrickColor.new (“Black”)
end

part.ClickDetector.MouseClick:Connect(OnClick)

So far, the brick turns black but I can’t make it turn back to fossil.
I know, this is extremely basic but I can’t figure out what to do, I appreciate any links/suggestions for me to look at to better my understanding of scripting as well. Thank you!

3 Likes

In this case you’re going to want to use variables, or as they are known in lua, locals. Let’s create a variable called “IsBlack” and set it’s value to false.

local IsBlack = false

Nice! Now, let’s make this value change when OnClick is fired. We can do this by adding this line inside of the function:

IsBlack = not IsBlack 

This will invert the value of IsBlack, changing it from false to true. This also works the other way around.

Finally, let’s make a couple of adjustments to OnClick:

function OnClick () 
  if IsBlack == false then
    part.BrickColor = BrickColor.new("Black")
  else
    part.BrickColor = BrickColor.new("Fossil")
  end
  IsBlack = not IsBlack 
end

With this new if statement, the function now can make the correct change based on the status of IsBlack. Great!

Hope this helped!

4 Likes
local color = "Fossil"
part = script.Parent

function OnClick()
   if color == "Fossil" then
      part.BrickColor = BrickColor.new (“Black”)
      color = "Black"
   elseif color == "Black" then
      part.BrickColor = BrickColor.new (“Fossil”)
      color = "Fossil"
   end

part.ClickDetector.MouseClick:Connect(OnClick)

This is a bit more inefficient, not the best solution, better for readability. Would reccomend 0skarians version.

2 Likes

Thank you for this though! It allows me to see how we can use Variables better such as **local color = “Fossil” which definitely helps my learning progress.

2 Likes

Ahh, right. Thank you for explaining the steps thoroughly. I will implement these types of variables into my scripting next time. This has certainly bettered my understanding of variables.

3 Likes

i am think maybe change that to RGB the color so i made this script for help you. how to do that: first add a part or mesh or everything what you just want and after that add a “ClickDetector” after that just add a script and copy the next code: script.Parent.ClickDetector.MouseClick:Connect(function(BlackPart)
script.Parent.Color = Color3.fromRGB(0, 0, 0)
end)

Did you know that you could shorten this further using (fake) ternary?

function OnClick () 
  part.BrickColor = BrickColor.new(IsBlack and "Black" or "Fossil")
  IsBlack = not IsBlack 
end

This doesn’t differ from the solution you wrote, just thought that was something cool to point out. Whatever is most readable for you should be selected.

1 Like

Oooh, that is really interesting. Thank you for teaching me that too. :blush: