This is the code for a module for a circuit breaker system in my building in the game. The code works fine, but I find it to be very bulky and can at times be unreliable in a large system.
This is the module that requires the most attention:
Code
function module.ON(breaker)
--script.Parent.O.Transparency = 1
--script.Parent.O.CD.MaxActivationDistance = 0
--script.Parent.N.Transparency = 0
--script.Parent.N.CD.MaxActivationDistance = 32
--== "NON-GENERATOR" ==--
print("::ON:: - "..typeof(breaker).." "..tostring(breaker))
if workspace.Electric:FindFirstChild(breaker).OR.Value == true then -- Won't turn on lights if "light switch" is off
print("::PASS:: - "..typeof(breaker).." "..tostring(breaker))
for _, v in pairs (workspace:GetChildren()) do if v.Name == "CLamp" and v:FindFirstChild(breaker) then
v.Lamp.SurfaceLight.Enabled = true
for _, vv in pairs(v:GetChildren()) do if vv:IsA("Part") and vv.Material == Enum.Material.Plastic then vv.Material = Enum.Material.Neon end end
elseif v.Name == "NewFlorLight" and v:FindFirstChild(breaker) then
v.Light.PointLight.Enabled = true
v.Light.SpotLight.Enabled = true
for _, vv in pairs(v:GetChildren()) do if vv:IsA("Part") and vv.Material == Enum.Material.Plastic then vv.Material = Enum.Material.Neon end end
elseif v.Name == "HangLight" and v:FindFirstChild(breaker) then
v.Light.PointLight.Enabled = true
v.Light.SpotLight.Enabled = true
for _, vv in pairs(v:GetChildren()) do if vv:IsA("Part") and vv.Material == Enum.Material.Plastic then vv.Material = Enum.Material.Neon end end
elseif v.Name == "PBLI" and v:FindFirstChild(breaker) then
v.Wire.SpotLight.Enabled = true
v.Wire.Material = Enum.Material.Neon
if v.Wire:FindFirstChild("SurfaceLight") then v.Wire.SurfaceLight.Enabled = true end
elseif v.Name == "FlatLight" and v:FindFirstChild(breaker) then
v.Light.Material = Enum.Material.Neon
v.Light.SpotLight.Enabled = true
elseif v.Name == "SpotLight" and not v:IsA("SpotLight") and v:FindFirstChild(breaker) then
for _, vv in pairs(v:GetChildren()) do if v:FindFirstChild("Bulb") then vv.Bulb.SpotLight.Enabled = true
if vv.Bulb.Material == Enum.Material.Plastic then vv.Bulb.Material = Enum.Material.Neon end end end
end end
--== "GENERATOR" ==--
for _, v in pairs (workspace.Generator:GetChildren()) do if v.Name == "CLamp" and v:FindFirstChild(breaker) then
v.Lamp.SurfaceLight.Enabled = true
for _, vv in pairs(v:GetChildren()) do if vv:IsA("Part") and vv.Material == Enum.Material.Plastic then vv.Material = Enum.Material.Neon end end
elseif v.Name == "NewFlorLight" and v:FindFirstChild(breaker) then
v.Light.PointLight.Enabled = true
v.Light.SpotLight.Enabled = true
for _, vv in pairs(v:GetChildren()) do if vv:IsA("Part") and vv.Material == Enum.Material.Plastic then vv.Material = Enum.Material.Neon end end
elseif v.Name == "HangLight" and v:FindFirstChild(breaker) then
v.Light.PointLight.Enabled = true
v.Light.SpotLight.Enabled = true
for _, vv in pairs(v:GetChildren()) do if vv:IsA("Part") and vv.Material == Enum.Material.Plastic then vv.Material = Enum.Material.Neon end end
elseif v.Name == "PBLI" and v:FindFirstChild(breaker) then
v.Wire.SpotLight.Enabled = true
v.Wire.Material = Enum.Material.Neon
if v.Wire:FindFirstChild("SurfaceLight") then v.Wire.SurfaceLight.Enabled = true end
elseif v.Name == "FlatLight" and v:FindFirstChild(breaker) then
v.Light.Material = Enum.Material.Neon
v.Light.SpotLight.Enabled = true
elseif v.Name == "SpotLight" and not v:IsA("SpotLight") and v:FindFirstChild(breaker) then
for _, vv in pairs(v:GetChildren()) do if v:FindFirstChild("Bulb") then vv.Bulb.SpotLight.Enabled = true
if vv.Bulb.Material == Enum.Material.Plastic then vv.Bulb.Material = Enum.Material.Neon end end end
workspace.Electric:FindFirstChild(breaker).Value = true
end end
else
workspace.Electric:FindFirstChild(breaker).Value = true
end
end
function module.OFF(breaker)
print("::OFF:: - "..typeof(breaker).." "..tostring(breaker))
-- script.Parent.N.Transparency = 1
-- script.Parent.N.CD.MaxActivationDistance = 0
-- script.Parent.O.Transparency = 0
-- script.Parent.O.CD.MaxActivationDistance = 32
for _, v in pairs (workspace:GetChildren()) do if v.Name == "CLamp" and v:FindFirstChild(breaker) then
v.Lamp.SurfaceLight.Enabled = false
for _, vv in pairs(v:GetChildren()) do if vv:IsA("Part") and vv.Material == Enum.Material.Neon then vv.Material = Enum.Material.Plastic end end
elseif v.Name == "NewFlorLight" and v:FindFirstChild(breaker) then
v.Light.PointLight.Enabled = false
v.Light.SpotLight.Enabled = false
for _, vv in pairs(v:GetChildren()) do if vv:IsA("Part") and vv.Material == Enum.Material.Neon then vv.Material = Enum.Material.Plastic end end
elseif v.Name == "HangLight" and v:FindFirstChild(breaker) then
v.Light.PointLight.Enabled = false
v.Light.SpotLight.Enabled = false
for _, vv in pairs(v:GetChildren()) do if vv:IsA("Part") and vv.Material == Enum.Material.Neon then vv.Material = Enum.Material.Plastic end end
elseif v.Name == "PBLI" and v:FindFirstChild(breaker) then
v.Wire.SpotLight.Enabled = false
v.Wire.Material = Enum.Material.SmoothPlastic
if v.Wire:FindFirstChild("SurfaceLight") then v.Wire.SurfaceLight.Enabled = false end
elseif v.Name == "FlatLight" and v:FindFirstChild(breaker) then
v.Light.Material = Enum.Material.SmoothPlastic
v.Light.SpotLight.Enabled = false
elseif v.Name == "SpotLight" and not v:IsA("SpotLight") and v:FindFirstChild(breaker) then
for _, vv in pairs(v:GetChildren()) do if v:FindFirstChild("Bulb") then vv.Bulb.SpotLight.Enabled = false
if vv.Bulb.Material == Enum.Material.Neon then vv.Bulb.Material = Enum.Material.Plastic end end end
end end
--== "GENERATOR" ==--
for _, v in pairs (workspace.Generator:GetChildren()) do if v.Name == "CLamp" and v:FindFirstChild(breaker) then
v.Lamp.SurfaceLight.Enabled = false
for _, vv in pairs(v:GetChildren()) do if vv:IsA("Part") and vv.Material == Enum.Material.Neon then vv.Material = Enum.Material.Plastic end end
elseif v.Name == "NewFlorLight" and v:FindFirstChild(breaker) then
v.Light.PointLight.Enabled = false
v.Light.SpotLight.Enabled = false
for _, vv in pairs(v:GetChildren()) do if vv:IsA("Part") and vv.Material == Enum.Material.Neon then vv.Material = Enum.Material.Plastic end end
elseif v.Name == "HangLight" and v:FindFirstChild(breaker) then
v.Light.PointLight.Enabled = false
v.Light.SpotLight.Enabled = false
for _, vv in pairs(v:GetChildren()) do if vv:IsA("Part") and vv.Material == Enum.Material.Neon then vv.Material = Enum.Material.Plastic end end
elseif v.Name == "PBLI" and v:FindFirstChild(breaker) then
v.Wire.SpotLight.Enabled = false
v.Wire.Material = Enum.Material.SmoothPlastic
if v.Wire:FindFirstChild("SurfaceLight") then v.Wire.SurfaceLight.Enabled = false end
elseif v.Name == "FlatLight" and v:FindFirstChild(breaker) then
v.Light.Material = Enum.Material.SmoothPlastic
v.Light.SpotLight.Enabled = false
elseif v.Name == "SpotLight" and not v:IsA("SpotLight") and v:FindFirstChild(breaker) then
for _, vv in pairs(v:GetChildren()) do if v:FindFirstChild("Bulb") then vv.Bulb.SpotLight.Enabled = false
if vv.Bulb.Material == Enum.Material.Neon then vv.Bulb.Material = Enum.Material.Plastic end end end
end end
workspace.Electric:FindFirstChild(breaker).Value = false
end
return module
yes I know its a lot of code
Here is an RBXL file with the module being used in context.
Please read the Instructions script in the workspace before asking questions. It gives an in-depth look at how the system works
CodeReviewBreaker.rbxl (48.4 KB)