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

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.

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

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

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

Answer this question