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!
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
)
@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)