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

Is there an easier way for this script to turn on multiple lights?

Asked by 3 years ago
Edited 3 years ago

I have a script that can turn on multiple lights in-game by clicking on a switch. However, I was wondering if there was an easier way to turn on the lights without needing to change each line of code when adding a new light. Here is the script:

local Light = script.Parent.Light
local Light2 = script.Parent.Light2
local Light3 = script.Parent.Light3
local Light4 = script.Parent.Light4
local Light5 = script.Parent.Light5
local Light6 = script.Parent.Light6
local Light7 = script.Parent.Light7
local Light8 = script.Parent.Light8 -- I don't want to keep repeating the same line of code each time I add a new light.
local Light9 = script.Parent.Light9
local Light10 = script.Parent.Light10
local LightSwitch = script.Parent.LightSwitch

local LightPart = Light.LightPart
local LightPart2 = Light2.LightPart2
local LightPart3 = Light3.LightPart3
local LightPart4 = Light4.LightPart4
local LightPart5 = Light5.LightPart5
local LightPart6 = Light6.LightPart6
local LightPart7 = Light7.LightPart7
local LightPart8 = Light8.LightPart8
local LightPart9 = Light9.LightPart9
local LightPart10 = Light10.LightPart10

local ClickHere = LightSwitch.ClickHere.ClickDetector

local SwitchA = LightSwitch.SwitchA 
local SwitchB = LightSwitch.SwitchB



ClickHere.MouseClick:connect(function() -- Function to turn on lights
    if Light.LightPart.SpotLight.Enabled == false then
        Light.LightPart.SpotLight.Enabled = true
        Light.LightPart.Material = Enum.Material.Neon
        Light2.LightPart2.SpotLight.Enabled = true
        Light2.LightPart2.Material = Enum.Material.Neon
        Light3.LightPart3.SpotLight.Enabled = true
        Light3.LightPart3.Material = Enum.Material.Neon
        Light4.LightPart4.SpotLight.Enabled = true
        Light4.LightPart4.Material = Enum.Material.Neon
        Light5.LightPart5.SpotLight.Enabled = true
        Light5.LightPart5.Material = Enum.Material.Neon
        Light6.LightPart6.SpotLight.Enabled = true
        Light6.LightPart6.Material = Enum.Material.Neon
        Light7.LightPart7.SpotLight.Enabled = true
        Light7.LightPart7.Material = Enum.Material.Neon
        Light8.LightPart8.SpotLight.Enabled = true
        Light8.LightPart8.Material = Enum.Material.Neon
        Light9.LightPart9.SpotLight.Enabled = true
        Light9.LightPart9.Material = Enum.Material.Neon
        Light10.LightPart10.SpotLight.Enabled = true
        Light10.LightPart10.Material = Enum.Material.Neon

        LightSwitch.SwitchA.Transparency = 0
        LightSwitch.SwitchB.Transparency = 1
    else
        Light.LightPart.SpotLight.Enabled = false -- Function to turn off lights
        Light.LightPart.Material = Enum.Material.SmoothPlastic
        Light2.LightPart2.SpotLight.Enabled = false
        Light2.LightPart2.Material = Enum.Material.SmoothPlastic
        Light3.LightPart3.SpotLight.Enabled = false
        Light3.LightPart3.Material = Enum.Material.SmoothPlastic
        Light4.LightPart4.SpotLight.Enabled = false
        Light4.LightPart4.Material = Enum.Material.SmoothPlastic
        Light5.LightPart5.SpotLight.Enabled = false
        Light5.LightPart5.Material = Enum.Material.SmoothPlastic
        Light6.LightPart6.SpotLight.Enabled = false
        Light6.LightPart6.Material = Enum.Material.SmoothPlastic
        Light7.LightPart7.SpotLight.Enabled = false
        Light7.LightPart7.Material = Enum.Material.SmoothPlastic
        Light8.LightPart8.SpotLight.Enabled = false
        Light8.LightPart8.Material = Enum.Material.SmoothPlastic
        Light9.LightPart9.SpotLight.Enabled = false
        Light9.LightPart9.Material = Enum.Material.SmoothPlastic
        Light10.LightPart10.SpotLight.Enabled = false
        Light10.LightPart10.Material = Enum.Material.SmoothPlastic  
        LightSwitch.SwitchA.Transparency = 1 -- This is just for the switch animation.
        LightSwitch.SwitchB.Transparency = 0
    end
end)

Here is what it looks like in Workspace: https://prnt.sc/t7spg3

0
Maybe you can use CollectionService, but I never use it before so :b https://developer.roblox.com/en-us/api-reference/class/CollectionService Block_manvn 395 — 3y
1
Yes that can work, but this is simple enough that local Lights = theFolder:GetChildren() is enough, i suggest using CollectionService if the objects that need to be grouped in a table together are all over the place in the explorer windows, but since the Lights can just be put in a folder together, :GetChildren() should be enough Azure_Kite 885 — 3y
0
You could make a table of the lights 3F1VE 257 — 3y

2 answers

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

Use for loop and table

Put all the lights inside a Folder or Model (I prefer folder) , which you can create on the explorer window, and put all the lights inside, just to keep things clean

local Lights = theFolder:GetChildren()
 -- This will put every reference to all objects inside the folder in a table
local isOn = Lights[0].LightPart.SpotLight.Enabled 
-- This will be used to determine wether the lights are on or off
local ClickHere = script.Parent.LightSwitch.ClickHere.ClickDetector
local SwitchA = script.Parent.LightSwitch.SwitchA
local SwitchB = script.Parent.LightSwitch.SwitchB

ClickHere.MouseClick:connect(function()
    if isOn then -- Check if its on
        isOn = false
        for _, v in pairs(Lights) do 
        -- Loops through the "Lights" table, if you are not sure how for loops works, go watch          YT videos on it, there are tons
            v.SpotLight.Enabled = false
            v.SpotLight.Material = Enum.Material.SmoothPlastic
        end
        SwitchA.Transparency = 1
        SwitchB.Transparency = 0
    else
        isOn = true
        for _, v in pairs(Lights) do 
            v.SpotLight.Enabled = true
            v.SpotLight.Material = Enum.Material.Neon
        end
        SwitchA.Transparency = 0
        SwitchB.Transparency = 1
    end
end)
Ad
Log in to vote
1
Answered by 3 years ago

Use an in pairs loop. This will loop through a table of whatever objects or values you provide. For example:

local playersTable = { -- table

    "Zyleak",
    "UristMcSparks",
    "Stickmasterluke"

}

game.Players.PlayerAdded:Connect(function(player)

    for i, v in pairs(playersTable) do
        if player.Name == v then
            player:Kick()
        end
    end

end)

You can place anything in a table. In this example, I was providing strings which would be used to identify players who I want to ban from my game because these are their usernames. Of course, it is much better to use their Roblox ids instead of their usernames but I just want this to be easier to understand.

On line 11, i means index. The position place of an object in the table is their index. The index of "Zyleak" is 1 because that is the first object in the table. The index of "UristMcSparks" is 2. Finally, the index of "Stickmasterluke" is 3. v, however, means value. The index shows where an object is in the table, but the value is self explanatory I guess. Value is value. It's whatever it is. Then, I told the loop to loop through the playersTable by placing it as a parameter in "pairs". After this line, you can do whatever you want to each value it loops through. I made it loop through the playersTable to check if the player's name was one of the string values I provided in the table. If it does match one of the string values, it will kick that player.

Now for your situation, loop through your table of lights as well. This is going to be more complicated though because of the names you gave each object. I will explain what I'm doing in comments.

 ClickHere.MouseClick:Connect(function() -- Function to turn on lights
    if Light.LightPart.SpotLight.Enabled == false then
    Light.LightPart.SpotLight.Enabled = true
    Light.LightPart.Material = Enum.Material.Neon
        for i, v in pairs(script.Parent:GetChildren()) do
        if v.Name ~= "Light" then -- making sure it's not Light because we already changed Light. I couldn't add Light in the loop because it doesn't have a number so this part would error
            local number = string.match(""..v.Name, "%d+") -- finds number in the light's name
            local childName = "LightPart"..number
            v[childName].SpotLight.Enabled = true
        v[childName].Material = Enum.Material.Neon
        end
    end
        LightSwitch.SwitchA.Transparency = 0
        LightSwitch.SwitchB.Transparency = 1

    else
        Light.LightPart.SpotLight.Enabled = false -- Function to turn off lights
        Light.LightPart.Material = Enum.Material.SmoothPlastic
        for i, v in pairs(script.Parent:GetChildren()) do
        if v.Name ~= "Light" then
            local number = string.match(""..v.Name, "%d+") -- finds number in the light's name
            local childName = "LightPart"..number
            v[childName].SpotLight.Enabled = false
        v[childName].Material = Enum.Material.SmoothPlastic
        end
    end
        LightSwitch.SwitchA.Transparency = 1 -- This is just for the switch animation.
        LightSwitch.SwitchB.Transparency = 0
    end
end)

Remember to capitalize the C in Connect, as just "connect" is deprecated. Feel free to fix this script if needed, and if you have any questions, I'll try to answer as soon as I can.

Answer this question