I made a code and it works. The code turned my lights in my building on and off, for the first floor. I have 4 other floors to do. the code is long, I had to rename each file a different name and add a line of code for each part.
Example:
On the first floor there are 91 lights. ( I had to rename each)
In each light there are 3 bulbs with a PoointLight in each one.( I had to rename each)(Light1,Light2..)
Now, on the other floors, I have each light that contains 17 parts in a Model (Light).
I have all 91 Lights in a model (SecondFloorLights,ThirdFloorLights...)
I need a code that will go in and get the 3 parts out of each of the 91 lights in each of the floor folders and when I hit the switch turn them on by setting the Brightness to 15. and turn them on by setting the Brightness by 0.
Here is an small example of the code that I have written.
local ison = false script.Parent.CFrame = script.Parent.CFrame * CFrame.fromEulerAnglesXYZ(0, 0, 0) function switch() if (ison == false) then ison = true script.Parent.CFrame = script.Parent.CFrame * CFrame.fromEulerAnglesXYZ(0, 2, 0) script.Parent.Parent.Parent.Light1.Bulb.PointLight.Brightness = 15 script.Parent.Parent.Parent.Light1.Bulb1.PointLight.Brightness = 15 script.Parent.Parent.Parent.Light1.Bulb2.PointLight.Brightness = 15 script.Parent.Parent.Parent.Light10.Bulb.PointLight.Brightness = 15 script.Parent.Parent.Parent.Light10.Bulb1.PointLight.Brightness = 15 script.Parent.Parent.Parent.Light10.Bulb2.PointLight.Brightness = 15 ---there is one group for each of the 91 lights(both on the top and bottom). else ison = false script.Parent.CFrame = script.Parent.CFrame * CFrame.fromEulerAnglesXYZ(0, -2, 0) script.Parent.Parent.Parent.Light1.Bulb.PointLight.Brightness = 0 script.Parent.Parent.Parent.Light1.Bulb1.PointLight.Brightness = 0 script.Parent.Parent.Parent.Light1.Bulb2.PointLight.Brightness = 0 script.Parent.Parent.Parent.Light10.Bulb.PointLight.Brightness = 0 script.Parent.Parent.Parent.Light10.Bulb1.PointLight.Brightness = 0 script.Parent.Parent.Parent.Light10.Bulb2.PointLight.Brightness = 0 end end script.Parent.ClickDetector.MouseClick:connect(switch)
If someone could help me with a shorter script I would love that. ( mine is 772 lines long)LOL...
OK, this is what I have come up with based off of and example, but its not working.
Any suggestion's?
local ison = false script.Parent.CFrame = script.Parent.CFrame * CFrame.fromEulerAnglesXYZ(0, 0, 0) function switch() if (ison == false) then ison = true script.Parent.CFrame = script.Parent.CFrame * CFrame.fromEulerAnglesXYZ(0, 2, 0) else ison = false script.Parent.CFrame = script.Parent.CFrame * CFrame.fromEulerAnglesXYZ(0, -2, 0) end end if (ison == true) then for i,v in pairs(script.Parent.Parent.Parent:GetChildren()) do -- for each Light do: for j,k in pairs(v:GetChildren()) do -- for each Bulb do: k.PointLight.Brightness = 15 -- Change the PointLight.Brightness end end else if (ison == false) then for i,v in pairs(script.Parent.Parent.Parent:GetChildren()) do -- for each Light do: for j,k in pairs(v:GetChildren()) do -- for each Bulb do: k.PointLight.Brightness = 0 -- Change the PointLight.Brightness end end end script.Parent.ClickDetector.MouseClick:connect(switch)
Whenever you find yourself doing a repetitive task, remember that you can almost surely use loops (and/or functions) to make it go faster!
Here's the script with for loops:
local ison = false script.Parent.CFrame = script.Parent.CFrame * CFrame.fromEulerAnglesXYZ(0, 0, 0) local lightParent = script.Parent.Parent.Parent function SetBrightness(brightness) for light = 1, 91 do for bulb = 1, 3 do local obj = lightParent["Light" .. light]["Bulb" .. (bulb == 3 and "" or bulb)] obj.PointLight.Brightness = brightness end end end function switch() if (ison == false) then ison = true script.Parent.CFrame = script.Parent.CFrame * CFrame.fromEulerAnglesXYZ(0, 2, 0) SetBrightness(15) else ison = false script.Parent.CFrame = script.Parent.CFrame * CFrame.fromEulerAnglesXYZ(0, -2, 0) SetBrightness(0) end end script.Parent.ClickDetector.MouseClick:connect(switch)
As you can see, a simple set of for loops can access all your objects without you have to type everything out.
Since you said you had 91 lights, each having 3 bulbs, we use two for loops. Next, note that game.Workspace.Something
works just as well as game.Workspace["Something"]
, but sometimes we need to use the 2nd format if we want to include variables, numbers, or spaces. So, to access the proper Light model, we use lightParent["Light" .. light]
. "Light" .. light
appends the number 'light' to the string "Light"; ex, if we're on the first loop, light = 1, so this will give us model "Light1". The next part of the line does the same thing for the Bulb, but with an "inline if" (known as a Ternary Operator, see here ). Mainly, since you used the names "Bulb", "Bulb1", and "Bulb2", I told it that if "bulb == 3", don't append anything instead of appending the number.
I am guessing that you renamed everything for the sole purpose of identifying each one. For the future, this is not required. Instead of using a for loop, you can simply scan for models that have a specific name:
local ch = lightParent:GetChildren() for i = 1, #ch do if ch[i].Name == "Light" then local lightCh = ch[i]:GetChildren() for j = 1, #lightCh do if lightCh[j].Name == "Bulb" then lightCh[j].PointLight.Brightness = brightness end end end end
That bit of code goes through all models named Light and changes the brightness of all PointLights in bricks named "Bulb".
Since you've gone to the trouble of differentiating all the lights, I'd personally try adding a "wait()" command, for graphical effect (only looks good, I imagine, if you had a pattern you used while naming everything):
function SetBrightness(brightness) for light = 1, 91 do for bulb = 1, 3 do local obj = lightParent["Light" .. light]["Bulb" .. (bulb == 3 and "" or bulb)] obj.PointLight.Brightness = brightness end wait() end end
(If you like the effect but want it to take longer, move the wait to the inner loop. If it takes too long, try "if light % 2 == 0 then wait() end", increasing the "2" to make it wait even less.)
What went wrong with your script? I'm guessing (since you didn't provide an error message) that you either have more than just "Light"s in script.Parent.Parent.Parent, or else your lights have more than just the 3 Bulbs in them (ie they probably have other "Part"s). Those other parts probably don't have PointLights in them, but your script didn't test for that -- it assumed that all children would have the PointLight.
that is annoying i'm sure haha. well all i can suggest are variables (local or global depending on what you need it for, but no point in making so many local variables) or tables (annex the code from the table using brackets [])