Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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.

1 comment

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.

3 answers

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.

7 comments

0

Thank you so much.

0

Np

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

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

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

0

if you saw the catastrophe i'm creating right now i think you would die

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

1 comment

0

your loops do one iteration too many

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

This discussion is preserved as it was. New answers, comments, and votes are closed.