Ok, to most people this will not be complicated but for me it really is.
I have this script and it works fine when turning on and off. But I want it to fade when turning on and turning off. I do not know how to do this though with this type of script.
Here is the script...
local P = script.Parent.Parent local LP1 = P.Light12.Light.SpotLight local LP2 = P.Light11.Light.SpotLight local LP3 = P.Light10.Light.SpotLight local LP4 = P.Light9.Light.SpotLight local LP5 = P.Light8.Light.SpotLight local LP6 = P.Light7.Light.SpotLight local LP7 = P.Light6.Light.SpotLight local LP8 = P.Light5.Light.SpotLight local LP9 = P.Light4.Light.SpotLight local LP10 = P.Light3.Light.SpotLight local LP11 = P.Light2.Light.SpotLight local LP12 = P.Light1.Light.SpotLight local LP13 = P.Light13.Light.SpotLight local LP14 = P.Light14.Light.SpotLight local LP15 = P.Light15.Light.SpotLight local LP16 = P.Light16.Light.SpotLight local LP17 = P.Light17.Light.SpotLight local LP18 = P.Light18.Light.SpotLight local LP19 = P.Light19.Light.SpotLight local LP20 = P.Light20.Light.SpotLight local LP21 = P.Light21.Light.SpotLight local LP22 = P.Light22.Light.SpotLight local LP23 = P.Light23.Light.SpotLight lp = {LP1, LP2, LP3, LP4, LP5, LP6, LP7, LP8, LP9, LP10, LP11, LP12, LP13, LP14, LP15, LP16, LP17, LP18, LP19, LP20, LP21, LP22, LP23} btn = script.Parent.ClickDetector function onClick() for i, v in pairs(lp) do v.Enabled = not v.Enabled end end btn.MouseClick:connect(onClick)
Eliminating 25 lines of difficult to type code
The way you are defining lp
is pretty scary. You don't need so much code!
We can use a simple loop over GetChildren
to make the list without having you type out all of the names! These few lines (which also name no names!) replace the first 25 lines of the code you posted:
local P = script.Parent.Parent; -- Same as before local lp = {}; for _,object in pairs(P:GetChildren()) do -- object is a child of P if object:FindFirstChild("Light") and object.Light:FindFirstChild("SpotLight") then table.insert(lp, object.Light.SpotLight); end end
The fade in/out is actually a moderate annoyance to program, because it is triggered by an event but continues working a time after that. In order for it to work really well, you would have to make sure multiple events don't interfere with each other, which means the events themselves probably shouldn't be doing the fading*.
*There's a solution to this that I will show here, but first I'll detail the solution without it first
Solution One
Our initial solution will have two parts: One is a loop constantly setting the brightness of every light. The second is a click event very similar to what you have.
local brightness = 0; local lightsOn = false; -- {and lp defined as from above} btn = script.Parent.ClickDetector function onClick() lightsOn = not lightsOn; -- Light switch end btn.MouseClick:connect(onClick) function updateBrightness() local oldBrightness = brightness; if lightsOn then brightness = math.min(1, brightness + 1 / (1 / .03)); -- The amount is so that the fade will take 1 second else brightness = math.max(0, brightness - 1 / (1 / .03)); end if brightness == oldBrightness then return true; -- For later; -- also avoids setting redundantly all of -- the values below which wastes time / -- network availability end for _,light in pairs(lp) do light.Brightness = brightness * 1; -- If max brightness should be more than 1, -- multiply by something bigger. end end while true do wait(0.03); updateBrightness(); end
This code will work as you would expect it would as a user no matter how much you click the on / off button. It will fade all the lights on / off together, which is what I'm assuming you wanted. (A very different approach is needed for separately...)
Improved Solution (Solution Two)
The downside to this is that we have this while true do
loop working constantly, even when things aren't changing; at the same time, we want to keep it so that if the light switch is pressed again, that click is registered and we don't get conflicts. Here is a solution to that problem:
local brightness = 0; local lightsOn = false; local controller = nil; function onClick() local myController = {}; controller = myController; lightsOn = not lightsOn; -- Light switch while controller == myController and not updateBrightness() do wait(0.03); end end -- (using same definition of `updateBrightness`as before)
What this does is run the loop until either
1) The brightness stops changing (all the way on or all the way off)
2) controller
is different from myController
: Since controller
is set to a new value whenever the button is clicked again, so this check is the same as "as long as it has not been clicked again"
I am very disappointed in the quality of answers I see on this site. Just because a script seems unimportant doesn't mean you should do it badly! If someone's asking for help, don't just sham something together, give the best answer you can give. If you aren't going to give the best answer they could receive then you probably aren't the person to give the answer..
If you want each and everyone one of them to fade at the same time, you're gonna need to use coroutines.
If not, then basically you'll need to do:
for i, v in pairs(lp) do if v.Brightness == 0 then while v.Brightness < 1 do v.Brightness = v.Brightness + 0.1 wait() end else while v.Brightness > 0 do v.Brightness = v.Brightness - 0.1 wait() end end