hey, I’m trying to make a light system in my game where there’s a breaker and blah blah blah.
the issue is, in the breaker function, enabled (the table) is not setting the light.enabled correctly, I printed the table a few times, and tried to print each time the loop went around again, it only printed once. I have no idea why this would happen. any help is appreciated
local CollectionService = game:GetService("CollectionService")
local LightsFolder = workspace.FamilyHome
local Enabled, LightsEnabled = {}, false
local Lights = {}
local function Breaker(On : boolean, SaveEnabled : boolean)
for _, Light in Lights do
if not On then
Enabled = {}
local Breaker = CollectionService:GetTagged("Breaker")[1].Parent
for _, BreakerLight in Breaker.Parent:FindFirstChild("Lights"):GetChildren() do
BreakerLight.Color = Color3.fromRGB(255,0,0)
end
if SaveEnabled then
Enabled[Light.Name] = Light.Enabled
end
Light.Enabled = false
else
local Breaker = CollectionService:GetTagged("Breaker")[1].Parent
for _, BreakerLight in Breaker.Parent:FindFirstChild("Lights"):GetChildren() do
BreakerLight.Color = Color3.fromRGB(0,255,0)
end
print(Enabled[Light.Name])
if SaveEnabled then
Light.Enabled = Enabled[Light.Name]
else
Light.Enabled = false
end
end
end
end
local function RegisterLight(Light : Light)
table.insert(Lights, Light)
Light:GetPropertyChangedSignal("Enabled"):Connect(function()
if Light.Enabled then
if Light.Parent.Name == "Light Part" then
Light.Parent.BrickColor = BrickColor.new("Institutional white")
elseif Light.Parent.Name == "Part" then
Light.Parent.Material = Enum.Material.Neon
end
else
if Light.Parent.Name == "Light Part" then
Light.Parent.BrickColor = BrickColor.new("Black")
elseif Light.Parent.Name == "Part" then
Light.Parent.Material = Enum.Material.Glass
end
end
end)
end
local function RegisterClickDetector(ClickDetector : ClickDetector)
ClickDetector.MouseClick:Connect(function()
if not LightsEnabled then return end
local GroupOfLights = ClickDetector:FindFirstChildOfClass("ObjectValue")
if GroupOfLights then
GroupOfLights = GroupOfLights.Value
for _, Light in GroupOfLights:GetDescendants() do
if Light:IsA("Light") then
Light.Enabled = not Light.Enabled
end
end
end
end)
end
local function RegisterBreakerClickDetector(ClickDetector : ClickDetector)
ClickDetector.MouseClick:Connect(function()
LightsEnabled = not LightsEnabled
Breaker(LightsEnabled, true)
end)
end
for _, Light in LightsFolder:GetDescendants() do
if Light:IsA("Light") then
RegisterLight(Light)
end
end
for _, ClickDetector in CollectionService:GetTagged("LightSwitch") do
RegisterClickDetector(ClickDetector)
end
for _, ClickDetector in CollectionService:GetTagged("Breaker") do
RegisterBreakerClickDetector(ClickDetector)
end
CollectionService:GetInstanceAddedSignal("LightSwitch"):Connect(RegisterClickDetector)
CollectionService:GetInstanceAddedSignal("Breaker"):Connect(RegisterBreakerClickDetector)
Breaker(false, false)