01 | while 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 ) |
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.
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.
01 | local lp = script.Parent.PointLight |
02 | local 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 |
14 | end |
15 |
16 | while true do |
17 | lightPointfade( true ) |
18 | lightPointfade( false ) |
19 | end |
Additional Information
I hope this helps, please comment if you do not understand how / why this code works.
01 | while 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 |
10 | end |
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:
1 | function 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 |
7 | end |
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
1 | function 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 |
7 | end |
8 |
9 | fade(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.
1 | function 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 |
9 | end |