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.

local Me = script.Parent

local H1 = game.Workspace.House_Lights.HouseLight_1.ReadInside.Light.Light.SpotLight
local H2 = game.Workspace.House_Lights.HouseLight_2.ReadInside.Light.Light.SpotLight
local H3 = game.Workspace.House_Lights.HouseLight_3.ReadInside.Light.Light.SpotLight
local H4 = game.Workspace.House_Lights.HouseLight_4.ReadInside.Light.Light.SpotLight
local H5 = game.Workspace.House_Lights.HouseLight_5.ReadInside.Light.Light.SpotLight
local H6 = game.Workspace.House_Lights.HouseLight_6.ReadInside.Light.Light.SpotLight
local H7 = game.Workspace.House_Lights.HouseLight_7.ReadInside.Light.Light.SpotLight
local H8 = game.Workspace.House_Lights.HouseLight_8.ReadInside.Light.Light.SpotLight
local H9 = game.Workspace.House_Lights.HouseLight_9.ReadInside.Light.Light.SpotLight

local AllHouse = H1, H2, H3, H4, H5, H6, H7, H8, H9 --WHERE THE H's ARE LISTED

local on_or_off = "off"

function onClicked ()
    if on_or_off == "off" then --If the house lights are off then
        game.Workspace.HouseLightButton.ClickDetector.MaxActivationDistance = 0
        AllHouse.Enabled = true
        Me.BrickColor = BrickColor.new("Dark green")        
        for i=0,60, 0.5 do 
            AllHouse.Brightness = i
            wait(0.25)
        end
        game.Workspace.HouseLightButton.ClickDetector.MaxActivationDistance = 32
        print("Lights are on. Rise and shine!")
        on_or_off = "on"
    else -- Otherwise, turn the houselights on
        game.Workspace.HouseLightButton.ClickDetector.MaxActivationDistance = 0
        Me.BrickColor = BrickColor.new("Bright red")
        for i=60,0, -0.5 do
            AllHouse.Brightness = i
            wait(0.25)
        end
        game.Workspace.HouseLightButton.ClickDetector.MaxActivationDistance = 32
        AllHouse.Enabled = false
        on_or_off = "off"
        print("Lights are off, goodnight.")
    end
end

game.Workspace.HouseLightButton.ClickDetector.MouseClick:connect(onClicked)

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:

local 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:

-- make a list:
local 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:

for i=60,0, -0.5 do
    for _, light in pairs(lights) do
        light.Brightness = i
    end
    wait(0.25)
end

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:

local lights = {} -- start with an empty list
for _, light in pairs(workspace.House_Lights:GetChildren()) do
    local spotLight = light.ReadInside.Light.Light.SpotLight
    table.insert(lights, spotLight) -- add the spot light to the `lights` list
end
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