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

Help with complicated script?

Asked by 10 years ago

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...

01local P = script.Parent.Parent
02local LP1 = P.Light12.Light.SpotLight
03local LP2 = P.Light11.Light.SpotLight
04local LP3 = P.Light10.Light.SpotLight
05local LP4 = P.Light9.Light.SpotLight
06local LP5 = P.Light8.Light.SpotLight
07local LP6 = P.Light7.Light.SpotLight
08local LP7 = P.Light6.Light.SpotLight
09local LP8 = P.Light5.Light.SpotLight
10local LP9 = P.Light4.Light.SpotLight
11local LP10 = P.Light3.Light.SpotLight
12local LP11 = P.Light2.Light.SpotLight
13local LP12 = P.Light1.Light.SpotLight
14local LP13 = P.Light13.Light.SpotLight
15local LP14 = P.Light14.Light.SpotLight
View all 34 lines...

2 answers

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

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:

1local P = script.Parent.Parent; -- Same as before
2local lp = {};
3for _,object in pairs(P:GetChildren()) do
4    -- object is a child of P
5    if object:FindFirstChild("Light") and
6    object.Light:FindFirstChild("SpotLight") then
7        table.insert(lp, object.Light.SpotLight);
8    end
9end

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.

01local brightness = 0;
02local lightsOn = false;
03-- {and lp defined as from above}
04 
05btn = script.Parent.ClickDetector
06function onClick()
07    lightsOn = not lightsOn; -- Light switch
08end
09btn.MouseClick:connect(onClick)
10 
11function updateBrightness()
12    local oldBrightness = brightness;
13    if lightsOn then
14        brightness = math.min(1, brightness + 1 / (1 / .03));
15        -- The amount is so that the fade will take 1 second
View all 35 lines...

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:

01local brightness = 0;
02local lightsOn = false;
03local controller = nil;
04function onClick()
05    local myController = {};
06    controller = myController;
07    lightsOn = not lightsOn; -- Light switch
08    while controller == myController and not updateBrightness() do
09        wait(0.03);
10    end
11end
12 
13-- (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..

0
Thanks alot! Really appreciate this. One quick question. For 'LP =' do I need to do script.Parent.Parent blagh blagh blagh or do I just need to put in Light1, Light2 etc..? kieranm9090 49 — 10y
0
For what I showed you, you just need your `P = ` and then the snippet I provided. It will fill in all of the Light1, Light2 etc so you don't need to type them all out! BlueTaslem 18071 — 10y
0
I edited it to include the full definition you need! Hopefully this makes sense BlueTaslem 18071 — 10y
0
I inserted all that into the script in the button but I have a problem. When I press the button again it does not turn back on. It turns off though kieranm9090 49 — 10y
View all comments (4 more)
0
I just tested it all; both solutions work perfectly for me... Are you understanding what you're pasting..? Because if not you might have picked things in a poor order? BlueTaslem 18071 — 10y
0
At the moment I am putting all of this into the script. I do it in the order you gave me, is it that I am not ment to put it all in one or should I? At this current time I am putting it all into one script. kieranm9090 49 — 10y
0
Pick only one of the Soln 1 or Soln 2 (but both use the same "updateBrightness" function BlueTaslem 18071 — 10y
0
The only problem is that it turns off but does not turn back on again! kieranm9090 49 — 10y
Ad
Log in to vote
-2
Answered by 10 years ago

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:

01for i, v in pairs(lp) do
02   if v.Brightness == 0 then
03      while v.Brightness < 1 do
04         v.Brightness = v.Brightness + 0.1
05         wait()
06      end
07   else
08      while v.Brightness > 0 do
09         v.Brightness = v.Brightness - 0.1
10         wait()
11      end
12end
0
I put that in the script but it did not work kieranm9090 49 — 10y
0
The light needs to be Enabled. NutsNWaffles 135 — 10y
0
They are kieranm9090 49 — 10y
0
You do NOT need coroutines to make them all fade at once. BlueTaslem 18071 — 10y

Answer this question