Lights switch scripts

Hi,

I am actually working on a jail map where i have multiple cells where each one has 3 lights switch buttons. It is a jail so there will be many cells.

I am wondering if i could optimize all the scripts i actually have to only one ? Or maybe 2 but not more than for example 40 scripts…

How could i do that ? How much options do i have ?

Thanks,

Arkola

1 Like

I think you can use the collection service. I don’t know much about this, but this is one way to solve your problem

1 Like

If your goal is simply to condense all of this into one script, then use a loop to add all the lighting functions.

1 Like

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.

Example with the Collection Service:

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

And here’s how it is organized in the workspace:

I hope it helped!

1 Like

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.