Help With Checking Colors of Parts

How would I go about doing something (e.x. printing something in the output) if all the lights in this script are bright green? I’m rather inexperienced at scripting so I apologize if this is just a really simple solution. (This is a local script in StarterPlayerScripts.)

local state = false

game.Workspace.switch1.ClickDetector.mouseClick:connect(function(touched)
    state =  not state
    if state then
	    game.Workspace.light1.BrickColor = BrickColor.new("Bright green")
	    game.Workspace.light2.BrickColor = BrickColor.new("Bright green")
	    game.Workspace.light3.BrickColor = BrickColor.new("Bright green")
    else
	    game.Workspace.light1.BrickColor = BrickColor.new("Medium stone grey")
	    game.Workspace.light2.BrickColor = BrickColor.new("Medium stone grey")
	    game.Workspace.light3.BrickColor = BrickColor.new("Medium stone grey")
    end
end)

game.Workspace.switch2.ClickDetector.mouseClick:connect(function(touched)
    state =  not state
    if state then
	    game.Workspace.light3.BrickColor = BrickColor.new("Bright green")
	    game.Workspace.light4.BrickColor = BrickColor.new("Bright green")
    else
	    game.Workspace.light3.BrickColor = BrickColor.new("Medium stone grey")
	    game.Workspace.light4.BrickColor = BrickColor.new("Medium stone grey")
    end
end)

game.Workspace.switch3.ClickDetector.mouseClick:connect(function(touched)
    state = not state
    if state then
	    game.Workspace.light4.BrickColor = BrickColor.new("Bright green")
	    game.Workspace.light5.BrickColor = BrickColor.new("Bright green")
    else
	    game.Workspace.light4.BrickColor = BrickColor.new("Medium stone grey")
	    game.Workspace.light5.BrickColor = BrickColor.new("Medium stone grey")
    end
end)

Any replies or help would be very much appreciated and I thank you in advance.

1 Like

So you would loop through all of the lights and check if they are green. Then if they are, you print.

local Lights = {workspace.light1, workspace.light2, etc..}

function CheckLights()
    local Check = 0
    for i, Light in pairs(Lights) do
        if Light.BrickColor ==  BrickColor.new("Bright green") then
            Check = Check + 1
            if Check == #Lights then
                print(“all lights green”)
            end
        end
    end
end

You can call CheckLights() whenever you want to check if the lights are all green.

5 Likes

He can put all of the lights in a model, to then use ModelName:GetChildren(), in the for loop.

This was just for the sake of things for beginners. Loops are learned before these functions.

You forgot an = at here:

[30 char]

But works perfectly fine!

1 Like

Hm, I tried putting that script after all the mouse click functions but even when all the lights were green, nothing was printed in output.

Did you call the function? [30 char]

Also, was state supposed to be a debounce?

Yes, I called the function. [30 chars]

[30 charssssssssssssssssssssssssss]

No actually, it was just a light turning on and off logic system that I implemented.

I will make a code for you, but for it to work, create a model and put all of the lights in it, or just put them in a folder.

This is odd, all lights green is printed in output if I call the function after the first “if state then.” Unfortunately, this only works for the first three lights though and not the other two.

I got it to work, thank you both for helping me so much! I just had to call the function in numerous areas.