How to turn all lights off from one button?

It’s pretty easy you can just use a mouse1buttondown function and disable all the lights in the workspace.

2 Likes

for i, v in pairs(lights:GetChildren()) do
if v:IsA(“PointLight”) or v:IsA(“SurfaceLight”) or v:IsA(“PointLight”) then
v.Enabled = false
end
end
-I’m on my phone sorry the lua formatting doesn’t work :frowning:

3 Likes

Where is your lights stored? I will make a script for you.

Well here you go:

button.MouseButton1Click:Connect(function()
for i, v in pairs(lights:GetChildren())
v.Enabled = true
end
end
4 Likes

So lets say you have 3 parts which have lights in them.

local Light1 = game.Workspace.Light1.Light --define your lights however you want 
local Light2 = game.Workspace.Light2.Light
local Light3 = game.Workspace.Light3.Light

You defined your light elements [not the parts]. Now, if you have a click detector in the part you want to click to turn off the lights [ switch ], you need to define the click detector and create the function.

local ClickDetector = game.Workspace.Switch.ClickDetector

ClickDetector.MouseButton1Click:Connect(function()
      --now basically turn the property called Active on false for each light element
     Light1.Active = false
     Light2.Active = false
     Light3.Active = false
end)

So now you can turn OFF the lights by clicking a part. But let’s say you want to turn them back on. Here you will need to use and play around with an If…then statement.

local ClickDetector = game.Workspace.Switch.ClickDetector
local LightsOpen = true
ClickDetector.MouseButton1Click:Connect(function()
    if LightsOpen == true then 
      --now basically turn the property called Active on false for each light element
     Light1.Active = false
     Light2.Active = false
     Light3.Active = false

     LightsOpen = false
    elseif LightsOpen == false then
   --now basically turn all the lights on, since they were closed
     Light1.Active = true
     Light2.Active = true
     Light3.Active = true
     
     LightsOpen = true
  end
end)

I know this is the long way but its easier to add/remove any lights you don’t want. And since you said you’re not such a good scripter, this will make you understand better.
If there are any errors in the script, don’t hesitate to come back and ask for help again.

You can also use for…in pairs statement like @happle5222 mentioned above. :heart:

5 Likes

Thank you for all your responses. I shall give some suggestions a try! :slight_smile:

1 Like

Hi there. Although there are many ways, to accomplish this, if you want to have a left click and a right click, you can use Mouse1ButtonDown to turn on, (Left Click) and Mouse2ButtonDown to turn off, (Right Click).
There is also an alternative. You can use an If, Then statement
For Example:

script.Parent.MouseButton1Click:Connect(function()
for i, v in pairs(lights:GetChildren())
if v.Enabled == true then
v.Enabled = false
end
else if v.Enabled == false then
v.Enabled = true
end
end
end)

Hope this helped! :smiley:

1 Like

I had this problem a while ago, couldn’t seem to find answers on the devforum, I am glad this is posted. This is a little off-topic but, the reason I am replying to @ii_G0N3 is because I want to ask the question: How do you define parts/items at the beginning of your script? I can’t seem to grasp that fundamental part of making scripts for some reason.

1 Like

So , do you see that dot? The dot basically means “is a member of”

local Part = game.Workspace.Part

So we created a local named Part [ you can name it however you want ] . That part is a member of Workspace, which is also a member of the game.

You can think about this both ways. But i honestly recommend to start from left to right.
The Part is located in the game, inside workspace

1 Like

Hi. I am going to attempt to do this now. The tricky thing is though I don’t have 3 light parts in the game, so would I have to type out every single one?

As i said, you can define each part. If there are many in a single folder you can copy and paste again and again and changing the locals in the end. Or you can do how @happle5222 said and use for…in pairs statement.

1 Like

Instead of doing copy paste of code, or looping trough folders and storages (Because that can cause a lot of performance issues), It will be much easier just using CollectionService and Tag Editor Plugin, to set all the lights of when the button is pressed.

Fast explanation if you don’t know what’s that:

CollectionService is a way to tag parts and get tagged parts, so you can execute scripts for lots of parts with a single script instead of using lots of scripts.

1 Like

@Alvin_Blox made a simple and cool tutorial about those 2 things

So if the part has parents, like a model or a folder, you would do how many parents it has till it has to get to workspace and then game? So if it had 1 parent it would be…

local Part = game.Workspace.Parent.Part

and so on? I also was told a while ago that you can name the “Parent” to what the item is called, so if the Parent was a model called Blah you can do…

local Part = game.Workspace.Blah.Part

Can you clarify the above topics?

(This should go to messages, just send a message with a response, don’t want to clutter the topic, but if it finds helpful to the OP then its fine.)

“A model is the parent of the part” means that the named part is inside the model. Thats what Parent means.

So you can use Parent only when you define an object from inside the model, outside.
For an example, you have a model which includes Part1 and a script. To define Part1 you can follow both ways :

From Workspace to Part

local Part = game.Workspace.Model.Part1

From Script to the Parent [ model ] and then to Part1

local Part = script.Parent.Part1
  • Parent = The Folder / Model / Part which contains a specific element.
    Example : “The door is a parent of the knob” => The knob is an element of the Door

Let’s say you have a Part in the folder named MainMenu as in the image below.
image
From TutorialScript you can define the part inside MainMenu in 2 ways. So as i said :

From Workspace to the Part

local RandomPart = game.Workspace.MainMenu.Part
--or
local RandomPart = Workspace.MainMenu.Part

From our script to the wanted Part

local RandomPart = script.Parent.Parent.Parent.Parent.Part
--                        Part   Inv    Lights Menu
--The script is an element of Part, which is an element of InvLights, which is an element of Lights. which is also an element of the MainMenu.

It might look and sound hard at first but if you keep defining things right and practice, you will have no problems!

In conclusion, you can use Parent term when defining elements from inside the model, to outside. It does NOT work from outside, inside. When defining things from outside, inside you need to name each element until you reach the part.

  • Example : image
--defining PointLight
local light = game.Workspace.Lamp.Light.PointLight
1 Like

I have not tried this yet, I am still building up the courage to do it as it all looks so complicated!!

Recently made a tutorial about all the functions and uses of CollectionService if you wanna check it out

1 Like

So is this a reason of why you should name your parts different names, like even a character difference because you will define a part you didn’t want to define?

BTW, Thanks so much for clearing this topic up for me, it means a lot.

Bookmarked and liked

If you have a lot of lights in various folders and you don’t want to create a variable for all of them, you can use CollectionService.

for _, v in ipairs(CollectionService:GetTagged("Light")) do
    v.Enabled = not v.Enabled
end

You have to tag all of your lights with “Light” first though.

Yes. Having 2 parts with the same name will “confuse” the script. Name each part differently to avoid this issue.
There is no problem when :

  • The parent of the part you want to define has the same name of the other part’s name
    Example : Two parts named Ragdoll are parents of two parts, Part1 and Part2
  • One of the defined part is in a different folder/model/part than the other part.

There will be an issue when :

  • The part you define has the same name of any part in the same folder/model/part
  • The parts you define are named identically
    Example :
local Part = game.Workspace.Model.Part
local RandomPart = game.Workspace.Model.Part
  • You define the same part two times. In this case, the script will take the last version which you have defined it
2 Likes