Anyway I can restructure this piece of code to be more optimized?

I usually don’t ever use for i loops, but in this situation it seems like it is my only solution. My goal is to tween a beam’s transparency, but tween doesn’t have the ability to tween beam transparency values directly. So the only way to do this is to use a for i loop to get a number value and replace it in the number sequence. Is there any other solution other than for i loops? If there is no other solution, then is a wait() always required in a for i loop? I also do not like wait().

for i = 0.5,1.01,0.01 do
            beam.Transparency = NumberSequence.new(i,i)
            wait()
        end

You can tween the transparency, you just have to put the Transparency in your goals table, and put the beam as the object by doing so:

local TweenService = game:GetService("TweenService")
local tweeninfo = TweenInfo.new(TIME, EASINGSTYLE, EASINGDIRECTION, REPEATCOUNT, REVERSES, DELAYTIME)
local goals = {Transparency = NUMBER}

local tween = TweenService:Create(BEAM, tweeninfo, goals)

tween:Play()

image
But beam transparency requires a number sequence? I don’t think you can directly tween the transparency.

I also tried using a NumberSequence.new() function, and says the property cannot be tweened. Then I would suggest a for loop is the way to go, unless there’s anything else out there.

While NumberSequences are not valid property that can be tweened, we can simply tween a separate number by tweening a numbervalue, and changing the beams transparency to its value. This is also a useful technique for tweening models CFrame e.t.c

local TweenService = game:GetService("TweenService")

local function TweenTransparency(Beam: Beam, Start: number, Goal: number)
	local NumberValue = Instance.new("NumberValue")
	NumberValue.Value = Start
	
	NumberValue.Changed:Connect(function(Value)
		Beam.Transparency = NumberSequence.new(Value)
	end)
	
	local Tween = TweenService:Create(NumberValue, TweenInfo.new(1), {Value = Goal})
	Tween:Play()
	
	Tween.Completed:Connect(function()
		NumberValue:Destroy()
		Tween:Destroy()
	end)
end

TweenTransparency(workspace.Beam, 0.5, 1)

https://gyazo.com/bb63250d8e2ac4a58251684416644494