Before i explain, No this is not a request, i am not requesting for anyone to create a script for me.
I am a beginner scripter so I'm not very knowledgeable in terms of using Lua. I've watched multiple tutorials on how to make a light with a switch, however, none of them have really gone about explaining is and how it could tun on multiple lights at once. If someone could give me some steps I could take in order to accomplish this I would really appreciate it since I'm at a loss of where to even begin.
script.Parent.ClickDetector.MouseClick:connect(function() if game.Workspace.Light.Light.PointLight.Enabled == false then game.Workspace.Light.Light.PointLight.Enabled = true game.Workspace.Light.Light.Material = "Neon" game.Workspace.lsw1.Union.Transparency = 1 game.Workspace.lsw1.ls2.Transparency = 0 else game.Workspace.Light.Light.PointLight.Enabled = false game.Workspace.Light.Light.Material = "Plastic" game.Workspace.lsw1.Union.Transparency = 0 game.Workspace.lsw1.ls2.Transparency = 1 end end)
Sure, So I'm assuming you're probably using a PointLight
with the Enabled
Property set as true
(for on) or false
for off.
If you want to call multiple parts, you pre-define them, such as
local light1 = workspace.Light1.PointLight local light2 = workspace.Light2.PointLight local light3 = workspace.Light3.PointLight
then write a function like the one below
workspace.Switch1.ClickDetector.MouseClick:Connect(function() if light1.Enabled == false then light1.Enabled = true light2.Enabled = true light3.Enabled = true elseif light1.Enabled == true then light1.Enabled = false light2.Enabled = false light3.Enabled = false end end)
Combined
local light1 = workspace.Light1.PointLight local light2 = workspace.Light2.PointLight local light3 = workspace.Light3.PointLight workspace.Switch1.ClickDetector.MouseClick:Connect(function() if light1.Enabled == false then light1.Enabled = true light2.Enabled = true light3.Enabled = true elseif light1.Enabled == true then light1.Enabled = false light2.Enabled = false light3.Enabled = false end end)
You can also define them within the function if you wanted to
workspace.Switch1.ClickDetector.MouseClick:Connect(function() if workspace.Light1.PointLight.Enabled == false then workspace.Light1.PointLight.Enabled = true workspace.Light2.PointLight.Enabled = true workspace.Light3.PointLight.Enabled = true elseif workspace.Light1.PointLight.Enabled == true then workspace.Light1.PointLight.Enabled = false workspace.Light2.PointLight.Enabled = false workspace.Light3.PointLight.Enabled = false end end)
EDIT: I'll append the script you added using the information above:
local detector = script.Parent.ClickDetector local light1 = workspace.Light.Light local model = workspace.Isw1 detector.MouseClick:Connect(function() if light1.PointLight.Enabled == false then light1.PointLight.Enabled = true light1.Material = "Neon" model.Union.Transparency = 1 model.ls2.Transparency = 0 else light1.PointLight.Enabled = false light1.Material = "Plastic" model.Union.Transparency = 0 model.ls2.Transparency = 1 end end)
To work with one or more Instances at once, you’d usually use a table. Then applying a special Loop to this table to traverse through each Instance, and amend the property accordingly. It would look somewhat like so (I will be using a KeyPress related Event to switch on the Lights)
local UserInputService = game:GetService("UserInputService") local lights = { workspace.Light1, workspace.Light2, workspace.Light3 }; UserInputService.InputBegan:Connect(function(InputObject) if (InputObject.KeyCode == Enum.KeyCode.E) then for _,light in ipairs(lights) do light.Enabled = (not light.Enabled) end end end)
I hope this was easy to understand, if not please comment and I’ll justify, good luck! Remember to accept and upvote if this helps!
This is an example, don’t expect to plug this in and have it work, instead you’ll have to configure this to be compatible with your desired end-game.
Alright this should work, its a bit complicated looking since the light components are in 2 locations, we have to run pairs twice to get both. As long as the lights are parented correctly - game.Workspace.Light.Light for the lights, and game.Workspace.lsw1 for the other components, it should be ok. If you have any errors try to correct them, if you cant let me know.
script.Parent.ClickDetector.MouseClick:Connect(function() if game.Workspace.Light.Light.PointLight.Enabled == false then for i, lights in pairs(game.Workspace.Light.Light:GetChildren()) do lights.PointLight.Enabled = true lights.Material = "Neon" end for i, lsw in pairs(game.Workspace.lsw1:GetChildren()) do lsw.Union.Transparency = 1 lsw.ls2.Transparency = 0 end else for i, lights in pairs(game.Workspace.Light.Light:GetChildren()) do lights.PointLight.Enabled = false lights.Material = "Plastic" end for i, lsw in pairs(game.Workspace.lsw1:GetChildren()) do lsw.Union.Transparency = 0 lsw.ls2.Transparency = 1 end end end)