Hello! To solve your problem, you should either use the Collection Service or use a more simple way, which is to put every lights inside one unique folder and then loop through this folder with a for loop.
local CollectionService = game:GetService("CollectionService")
for i, cell in pairs(workspace.Cells:GetChildren()) do
for i, child in pairs(cell:GetChildren()) do -- looping through the folder where are stored your lights
if child.Name == "Light" then
CollectionService:AddTag(child, "light_"..cell.Name) -- "light" is the tag
elseif child.Name == "LightSwitch" then
CollectionService:AddTag(child, "lightswitch_"..cell.Name)
end
end
end
for i, cell in pairs(workspace.Cells:GetChildren()) do
for i, child in pairs(CollectionService:GetTagged("lightswitch_"..cell.Name)) do
child.ClickDetector.MouseClick:Connect(function()
for i, child in pairs(CollectionService:GetTagged("light_"..cell.Name)) do
if not child:GetAttribute("Enabled") then
child:SetAttribute("Enabled", true)
child.PointLight.Enabled = true
else
child:SetAttribute("Enabled", false)
child.PointLight.Enabled = false
end
end
end)
end
end
You could have the light and light switch models stored as a sort of prefab in server storage.
These prefabs each have a script attached to them to control them.
Then you place nodes all around the map (one for light switch, one for light), and at runtime, loop through all the nodes and replace them with your appropriate prefabs.
This way, you only have one instance of both the light and light switch - making it easier to make changes and debug the lights.
Note: for the light switch to know which lights it provides power to, you could give each light switch node an object value, then when you replace the light switch node, you copy over the object value to the light switch.
Then in the light switch control script, it uses that object value to refer to the correct light.
If you want, I can make a testing place for this behaviour, as it’s easier to understand when you see as opposed to the wall of text I posted.
Additionally, if you’re familiar with OOP I’d suggest using that for this case, heres an old but great tutorial - All about Object Oriented Programming
Although it is quite complicated.