I was wondering if I could get any assistance. I want to know how to turn off and on all lights in my game using one button that turns them on and off basically.
I am not a scripter at all, but I can easily follow tutorials. I have looked on here and on YouTube to find out but I can’t seem to find anything that might be helpful.
You can use a for loop in pairs() and find all the spot lights in your game if that’s what you want.
Edit: You can’t ask for scripts here, I suggest you go to #collaboration
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
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.
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)
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.
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.
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.
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.
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 :
--defining PointLight
local light = game.Workspace.Lamp.Light.PointLight
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.