The following script is supposed to fade the lights off if they are on, and fade them on if they are off.
01 | local Me = script.Parent |
02 |
03 | local H 1 = game.Workspace.House_Lights.HouseLight_ 1. ReadInside.Light.Light.SpotLight |
04 | local H 2 = game.Workspace.House_Lights.HouseLight_ 2. ReadInside.Light.Light.SpotLight |
05 | local H 3 = game.Workspace.House_Lights.HouseLight_ 3. ReadInside.Light.Light.SpotLight |
06 | local H 4 = game.Workspace.House_Lights.HouseLight_ 4. ReadInside.Light.Light.SpotLight |
07 | local H 5 = game.Workspace.House_Lights.HouseLight_ 5. ReadInside.Light.Light.SpotLight |
08 | local H 6 = game.Workspace.House_Lights.HouseLight_ 6. ReadInside.Light.Light.SpotLight |
09 | local H 7 = game.Workspace.House_Lights.HouseLight_ 7. ReadInside.Light.Light.SpotLight |
10 | local H 8 = game.Workspace.House_Lights.HouseLight_ 8. ReadInside.Light.Light.SpotLight |
11 | local H 9 = game.Workspace.House_Lights.HouseLight_ 9. ReadInside.Light.Light.SpotLight |
12 |
13 | local AllHouse = H 1 , H 2 , H 3 , H 4 , H 5 , H 6 , H 7 , H 8 , H 9 --WHERE THE H's ARE LISTED |
14 |
15 | local on_or_off = "off" |
The problem I'm having is it only fades on ONE of the lights (that's H1). None of the others respond. I'm not going into the program and individually telling each and every single one to turn on because that would be ridiculous. If you need any more information about something, I'll be happy to comply.
It's a good instinct that you don't want to repeat yourself. Repeating code for each one is not the right way to approach this :)
This is not how tuples work:
1 | local AllHouse = H 1 , H 2 , H 3 , H 4 , H 5 , H 6 , H 7 , H 8 , H 9 |
When you have an assignment with commas, each corresponding thing on the left gets set to the corresponding thing on the right. In this statement, all of the ones after H1
are ignored.
There is no way to make one value be a 'merged' version of a bunch of values (like you wanted AllHouse
to be), where setting something on it sets something on all of the lights. Instead, you make a collection of lights and do something to each element of the collection:
1 | -- make a list: |
2 | local lights = { H 1 , H 2 , H 3 , H 4 , H 5 , H 6 , H 7 , H 8 , H 9 } |
Note the curly braces.
lights
is a list -- it does not have an .Brightness
property. Instead, you use a for
loop to do something for each light in
lights
:
1 | for i = 60 , 0 , - 0.5 do |
2 | for _, light in pairs (lights) do |
3 | light.Brightness = i |
4 | end |
5 | wait( 0.25 ) |
6 | end |
Instead of typing out the full amount to define each H1
to H9
, you could just organize your model so that only lights are in workspace.House_Lights
. Then you can more easily generate the lights
list:
1 | local lights = { } -- start with an empty list |
2 | for _, light in pairs (workspace.House_Lights:GetChildren()) do |
3 | local spotLight = light.ReadInside.Light.Light.SpotLight |
4 | table.insert(lights, spotLight) -- add the spot light to the `lights` list |
5 | end |