while true do script.Parent.PointLight.Brightness = 0 wait (0.1) script.Parent.PointLight.Brightness = 0.1 wait (0.1) script.Parent.PointLight.Brightness = 0.2 wait (0.1) script.Parent.PointLight.Brightness = 0.3 wait (0.1) script.Parent.PointLight.Brightness = 0.4 wait (0.1) script.Parent.PointLight.Brightness = 0.5 wait (0.1) script.Parent.PointLight.Brightness = 0.6 wait (0.1) script.Parent.PointLight.Brightness = 0.7 wait (0.1) script.Parent.PointLight.Brightness = 0.8 wait (0.1) script.Parent.PointLight.Brightness = 0.9 wait (0.1) script.Parent.PointLight.Brightness = 1 wait (0.1) script.Parent.PointLight.Brightness = 0.9 wait (0.1) script.Parent.PointLight.Brightness = 0.8 wait (0.1) script.Parent.PointLight.Brightness = 0.7 wait (0.1) script.Parent.PointLight.Brightness = 0.6 wait (0.1) script.Parent.PointLight.Brightness = 0.5 wait (0.1) script.Parent.PointLight.Brightness = 0.4 wait (0.1) script.Parent.PointLight.Brightness = 0.3 wait (0.1) script.Parent.PointLight.Brightness = 0.2 wait (0.1) script.Parent.PointLight.Brightness = 0.1 wait (0.1) script.Parent.PointLight.Brightness = 0 end
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.
local lp = script.Parent.PointLight local function lightPointfade(isIn) if isIn then for i=0, 1, 0.1 do lp.Brightness = i wait(0.1) end else for i=1, 0, -0.1 do lp.Brightness = i wait(0.1) end end end while true do lightPointfade(true) lightPointfade(false) end
Additional Information
I hope this helps, please comment if you do not understand how / why this code works.
while true do for i=0,10 do script.Parent.PointLight.Brightness = script.Parent.PointLight.Brightness + .1 wait(.1) end for i=0,10 do script.Parent.PointLight.Brightness = script.Parent.PointLight.Brightness - .1 wait(.1) end 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:
function fade(light) for i = 0,1,.1 do light.Brightness = i delay(.05,function() light.Brightness = light.Brightness-.1 end) wait(.1) end 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
function fade(light) for i = 0,1,.1 do light.Brightness = i delay(.05,function() light.Brightness = light.Brightness-.1 end) wait(.1) end end 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.
function fade(light) if light:IsA("Light") then for i = 0,1,.1 do light.Brightness = i delay(.05,function() light.Brightness = light.Brightness-.1 end) wait(.1) end end end