Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would one go about making a light switch tuning on multiple lights at once?

Asked by 5 years ago
Edited 5 years ago

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)
0
To explain how to do it, you more or less have to make the script.. do you have one that works for a single switch already so we can help show you what to change? DinozCreates 1070 — 5y
0
just added it Cantbeatmerbro 1 — 5y
0
Put every lightswitch in a table, and loop through each of them and if theyre off then turn them on LightModed 81 — 5y
0
Put every lightswitch in a table, and loop through each of them and if theyre off then turn them on LightModed 81 — 5y
0
Sir it's been seven days, has any of our answers worked? Ziffixture 6913 — 5y

3 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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)
0
Deprecated connect, shun the non believer! DinozCreates 1070 — 5y
0
Did you neg me -.- DinozCreates 1070 — 5y
0
no i didnt, that was someone else, i upvoted yours before actually SerpentineKing 3885 — 5y
0
i got 2 negs and 2 pos, i feel honored. this question was a feeding frenzy. DinozCreates 1070 — 5y
View all comments (7 more)
0
ikr, im sure they all work, so its the user's preference SerpentineKing 3885 — 5y
0
I’ve posted so many great answers and had all of them unacccepted to someone who’s answer got there first. I am dropping in my accepted-answered ratio. I hope this one makes it through, good luck everyone Ziffixture 6913 — 5y
0
at least you arent losing rep on a perfectly fine answer. if you're gonna neg me at least let me know who you are and why. DinozCreates 1070 — 5y
0
wouldn't that be a useful feature?! (edit1, below: I dont think he's accusing you, just letting people know, edit2: thats pretty telling) SerpentineKing 3885 — 5y
0
I didn’t ‘neg’ you Ziffixture 6913 — 5y
0
I just upvoted you Ziffixture 6913 — 5y
0
oh i didnt mean you faehren, i just meant in general. DinozCreates 1070 — 5y
Ad
Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

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.

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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)

Answer this question