Issue w/ Light Switch (all the lights in the house)

Goal: Clicking the light switch will turn it on/off

Script:

local ClickDetector = game.Workspace.School.LightSwitch.ClickDetector
local school = game.Workspace.School:GetDescendants()
local light = false

ClickDetector.MouseClick:Connect(function()
	if light == false then
		
		for _, light in ipairs(school) do
			if light:IsA("PointLight") or light:IsA("SpotLight") or light:IsA("SurfaceLight") then
				light.Enabled = true
				light = true
			end
		end
	end
	
	if light == true then
		for _, light in ipairs(school) do
			if light:IsA("PointLight") or light:IsA("SpotLight") or light:IsA("SurfaceLight") then
				light.Enabled = false
				light = false
			end
		end
	end
end)

I wasn’t sure how to do this so I just put what I thought would work, which obviously didn’t. If anyone has ideas for a new script that is better/changes that would be great!

1 Like

Use elseif for the second part of your code. Both of your if statements are running currently. Think about the logic flow of it:

  1. if light == false, then it will set light = true and turn on the lights.
  2. if light == true, (which was just set to true), and will turn off lights.

So you’re turning them on then off immediately.

Do something more like this:

if light == false then
   --- Turn them on
   ...
else
   -- Turn them off
   ...
end
1 Like

Instead of doing 2 if statements, I suggest using elseif.

Hello! Im seeing some issues in the code.

so to start theres no cooldown, which means players can flick the lights on and off without any cooldown (the issue here is that this can cause flashing lights which might hurt a player)

secondly the code is kind of confusing. Im gonna help write something that works (or thats suppose to work)

local switch = script.Parent
local cooldown = false --cooldown boolean
local toggle = false --Used to toggle lights on and off

local CD = switch.ClickDetector --The click detecor we'll be using
local delay = 3 --how long the cooldown lasts

ClickDetector.MouseClick:Connect(function()
if cooldown == false then
   cooldown = true
   if toggle == false then
      toggle = true
      for i, v in pairs(school:GetDescendants()) do
         if v:IsA("PointLight") then --Configure to your likinh
            v.Enabled = false
         end
      end
   else
      toggle = false
      for i, v in pairs(school:GetDescendants()) do
         if v:IsA("PointLight") then --Configure to your likinh
            v.Enabled = false
         end
      end
   end
   task.wait(delay)
   cooldown = false
end
)

hope this helps!

@sleitnick @Valkyrop Thanks for the response! I tried that, and turning the light off works, but when I click it again nothing happens.

The print statement for the on does not run. New code:

local ClickDetector = game.Workspace.School.LightSwitch.ClickDetector
local school = game.Workspace.School:GetDescendants()
local light = true

ClickDetector.MouseClick:Connect(function()
	if light == true then
		for _, light in ipairs(school) do
			if light:IsA("PointLight") or light:IsA("SpotLight") or light:IsA("SurfaceLight") then
				light.Enabled = false
				light = false
				print("off")
			end
		end

	elseif light == false then
		for _, light in ipairs(school) do
			if light:IsA("PointLight") or light:IsA("SpotLight") or light:IsA("SurfaceLight") then
				light.Enabled = true
				light = true
				print("on")
			end
		
		end

	end

end)

Do you guys happen to have any ideas?

Change your debounce name “light” to something else or change the name “light” in the loop

1 Like

The variable light cannot be both a boolean and a part that gets enabled, disabled.

1 Like

Ahh, thank you so much! It worked now