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

How Do I Make A Light Increase and Decrease More Efficiently?

local light = script.Parent.PointLight

while true do
	light.Brightness = 2
	wait(.1)
	light.Brightness = 2.2
	wait(.1)
	light.Brightness = 2.4
	wait(.1)
	light.Brightness = 2.6
	wait(.1)
	light.Brightness = 2.8
	wait(.1)
	light.Brightness = 3
	wait(.1)
	light.Brightness = 3.2
	wait(.1)
	light.Brightness = 3.4
	wait(.1)
	light.Brightness = 3.6
	wait(.1)
	light.Brightness = 3.8
	wait(.1)
	light.Brightness = 4
	wait(.1)
	light.Brightness = 4.2
	wait(.1)
	light.Brightness = 4.4
	wait(.1)
	light.Brightness = 4.6
	wait(.1)
	light.Brightness = 4.8
	wait(.1)
	light.Brightness = 5
	wait(.1)
	light.Brightness = 4.8
	wait(.1)
	light.Brightness = 4.6
	wait(.1)
	light.Brightness = 4.4
	wait(.1)
	light.Brightness = 4.2
	wait(.1)
	light.Brightness = 4
	wait(.1)
	light.Brightness = 3.8
	wait(.1)
	light.Brightness = 3.6
	wait(.1)
	light.Brightness = 3.4
	wait(.1)
	light.Brightness = 3.2
	wait(.1)
	light.Brightness = 3
	wait(.1)
	light.Brightness = 2.8
	wait(.1)
	light.Brightness = 2.6
	wait(.1)
	light.Brightness = 2.4
	wait(.1)
	light.Brightness = 2.2
	wait(.1)
	light.Brightness = 2
	wait(.1)
end

3 answers

Use tweening.

A) It'll produce smoother results

B) It can be reversed

C) Way more concise

local tween_service = game:GetService("TweenService")

local light = script.Parent.PointLight

local tween_info = TweenInfo.new(
	1,   -- time 
	Enum.EasingStyle.Linear, -- easingstyle
	Enum.EasingDirection.Out, -- easingdirection
	100, -- repeats (should probably go higher for infinite loop)
	true -- the magic parameter to make it reverse
)  -- you can tweak these settings of course (see wiki links below for help)


local light_tween = tween_service:Create(light, tween_info, {Brightness = 5})
light_tween:Play()

TweenService TweenInfo

3 comments

0

Before I discovered this, I used for loops. I still am not quite familiar with the tween syntax.

0

Before I discovered this, I used for loops. I still am not quite familiar with the tween syntax.

0

TweenService:Create is a very simple function really. Look through the Wiki page, but the example program at the bottom might be most helpful to you: https://developer.roblox.com/en-us/api-reference/function/TweenService/Create

Well you could just make a loop that changes the brightness by 0.2 each time.

local light = script.Parent.PointLight

while true do
    wait(0.001)
    local light = script.Parent.PointLight

-------------------------------------------------------------------

    local currentBrightness = light.Brightness
    wait(0.1)
    currentBrightness = currentBrightness + 0.2
end

If this solved your question please accept this answer.

2 comments

0

that wouldn't work because it would just continually increase the brightness and not decrease it at any given point

0

Oh yeah i forgot to add decrease...

while wait(0.1) do
	for i = 2,5,0.2 do
		script.Parent.PointLight.Brightness = i
		wait(0.1)
	end
	for i = 5,2,-0.2 do
		script.Parent.PointLight.Brightness = i
		wait(0.1)
	end
end

1 comment

0

works perfectly

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