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

Is there a more efficient way to change the brightness through this script?

Asked by 8 years ago
01while true do
02    script.Parent.PointLight.Brightness = 0
03    wait (0.1)
04    script.Parent.PointLight.Brightness = 0.1
05    wait (0.1)
06    script.Parent.PointLight.Brightness = 0.2
07    wait (0.1)
08    script.Parent.PointLight.Brightness = 0.3
09    wait (0.1)
10    script.Parent.PointLight.Brightness = 0.4
11    wait (0.1)
12    script.Parent.PointLight.Brightness = 0.5
13    wait (0.1)
14    script.Parent.PointLight.Brightness = 0.6
15    wait (0.1)
View all 43 lines...

Let's face it- this code is an eyesore, but it's the only way I knew how to change the brightness of the light every 0.1 seconds. I have an idea of how to change it that the code isn't 40 lines long, but I don't want to mess up the script. So what's a way to make this more efficient, if there is a way? Thanks.

0
So far I made the shortest fade code with 7 lines ayy. It was only as a challenge because I said I could make one in 5-7 lines to kingdom5. His answer is right. I just challenged myself. EzraNehemiah_TF2 3552 — 8y

3 answers

Log in to vote
2
Answered by 8 years ago
Edited 8 years ago

If you are typing out the same code multiple times then you need to change the design of the code by adding in function or loops where applicable to stop any code duplication.

This is just an example, there are better ways to do this.

01local lp = script.Parent.PointLight
02local function lightPointfade(isIn)
03    if isIn then
04        for i=0, 1, 0.1 do
05            lp.Brightness = i
06            wait(0.1)
07        end
08    else
09        for i=1, 0, -0.1 do
10            lp.Brightness = i
11            wait(0.1)
12        end        
13    end
14end
15 
16while true do
17    lightPointfade(true)
18    lightPointfade(false)
19end

Additional Information

Loops

Functions

I hope this helps, please comment if you do not understand how / why this code works.

0
Thank you so much. Dried_Umeboshi 15 — 8y
0
Np User#5423 17 — 8y
0
Also, if I wanted to put some sort of code before it (I want these lights to only spawn at night) how would I attach it to the code stated here? Just wondering Dried_Umeboshi 15 — 8y
View all comments (3 more)
0
This code could be much more shorter though. And when I say much shorter I mean about 5-7 lines. Oh, I see, "This is just an example, there are better ways to do this." EzraNehemiah_TF2 3552 — 8y
0
Also seeing that while true do code block with no wait inside of it is making me go insane, I know there's a wait inside of the function but it's like triggering me or something lol EzraNehemiah_TF2 3552 — 8y
0
if you saw the catastrophe i'm creating right now i think you would die Dried_Umeboshi 15 — 8y
Ad
Log in to vote
0
Answered by 8 years ago
01while true do
02    for i=0,10 do
03        script.Parent.PointLight.Brightness = script.Parent.PointLight.Brightness + .1
04        wait(.1)
05    end
06    for i=0,10 do
07        script.Parent.PointLight.Brightness = script.Parent.PointLight.Brightness - .1
08        wait(.1)
09    end
10end
0
your loops do one iteration too many BlueTaslem 18071 — 8y
Log in to vote
0
Answered by 8 years ago

While Kingdom's code is right, I'd like to make a function where you can fade in and fade out any gui in the whole game with a single function while keeping it compact and small.


I commented on kingdom's answer saying you could make it in 5-7 lines of code and so I challenged myself to do it and I got this:

1function fade(light)
2    for i = 0,1,.1 do
3        light.Brightness = i
4        delay(.05,function() light.Brightness = light.Brightness-.1 end)
5        wait(.1)
6    end
7end

Because there is 21 steps to get from 0 to 1 and back to 0. It does the loop half way and after exactly half of the operation is completed I start subtracting it in .1 second intervals.


Now all you need to do is to call the function

1function fade(light)
2    for i = 0,1,.1 do
3        light.Brightness = i
4        delay(.05,function() light.Brightness = light.Brightness-.1 end)
5        wait(.1)
6    end
7end
8 
9fade(workspace.Brick.PointLight)

You should go on the wiki and learn from there or take notes from this site. This is a really good resource and you should always learn from this site!



Hope it helps!


Extra Just incase you accidently put inside an object into the fade function which isn't a light then you can add these 2 lines of code.

1function fade(light)
2    if light:IsA("Light") then
3        for i = 0,1,.1 do
4            light.Brightness = i
5            delay(.05,function() light.Brightness = light.Brightness-.1 end)
6            wait(.1)
7        end
8    end
9end

Answer this question