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

Lights not turning on?

Asked by
Troidit 253 Moderation Voter
9 years ago

The following script is supposed to fade the lights off if they are on, and fade them on if they are off.

01local Me = script.Parent
02 
03local H1 = game.Workspace.House_Lights.HouseLight_1.ReadInside.Light.Light.SpotLight
04local H2 = game.Workspace.House_Lights.HouseLight_2.ReadInside.Light.Light.SpotLight
05local H3 = game.Workspace.House_Lights.HouseLight_3.ReadInside.Light.Light.SpotLight
06local H4 = game.Workspace.House_Lights.HouseLight_4.ReadInside.Light.Light.SpotLight
07local H5 = game.Workspace.House_Lights.HouseLight_5.ReadInside.Light.Light.SpotLight
08local H6 = game.Workspace.House_Lights.HouseLight_6.ReadInside.Light.Light.SpotLight
09local H7 = game.Workspace.House_Lights.HouseLight_7.ReadInside.Light.Light.SpotLight
10local H8 = game.Workspace.House_Lights.HouseLight_8.ReadInside.Light.Light.SpotLight
11local H9 = game.Workspace.House_Lights.HouseLight_9.ReadInside.Light.Light.SpotLight
12 
13local AllHouse = H1, H2, H3, H4, H5, H6, H7, H8, H9 --WHERE THE H's ARE LISTED
14 
15local on_or_off = "off"
View all 43 lines...

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.

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

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:

1local AllHouse = H1, H2, H3, H4, H5, H6, H7, H8, H9

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:
2local lights = {H1, H2, H3, H4, H5, H6, H7, H8, H9}

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:

1for i=60,0, -0.5 do
2    for _, light in pairs(lights) do
3        light.Brightness = i
4    end
5    wait(0.25)
6end

Less code to define your lights

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:

1local lights = {} -- start with an empty list
2for _, 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
5end
0
Thanks SOOO Much. Your explanation was perfect. I'm happy you also agree with me that repeating the same command is ridiculous. Troidit 253 — 9y
Ad

Answer this question