Any way how to optimize this heck long 615 line code?

I know… 615 lines… not the greatest you will see, but it works though.
How can I lower this script down to a few lines or atleast below 100 lines (if possible) ?
As far as I heard, the longer the script is, the more latency/ping, network recieve lowering and script performance lowering…

I’m still a beginner at Lua but I’m working on learning on how to optimize code based on other developers showing me how to.

while wait(0.05) do
	for i,v in pairs (game:GetService("Workspace").TaLights:GetChildren()) do
		spawn(function()
			if v.Name == "Ta12" or v.Name == "Ta13" then
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
				wait(0.001)
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 5
				v.Head.Lens.Transparency = .5
				v.Head.Beam.light.Transparency = NumberSequence.new(.5)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 7.5
				v.Head.Lens.Transparency = .25
				v.Head.Beam.light.Transparency = NumberSequence.new(.25)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 10
				v.Head.Lens.Transparency = 0
				v.Head.Beam.light.Transparency = NumberSequence.new(0)
			end
		end)
	end
	wait(0.01)
	for i,v in pairs (game:GetService("Workspace").TaLights:GetChildren()) do
		spawn(function()
			if v.Name == "Ta11" or v.Name == "Ta14" then
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
				wait(0.001)
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 5
				v.Head.Lens.Transparency = .5
				v.Head.Beam.light.Transparency = NumberSequence.new(.5)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 7.5
				v.Head.Lens.Transparency = .25
				v.Head.Beam.light.Transparency = NumberSequence.new(.25)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 10
				v.Head.Lens.Transparency = 0
				v.Head.Beam.light.Transparency = NumberSequence.new(0)
			end
		end)
	end
	for i,v in pairs (game:GetService("Workspace").TaLights:GetChildren()) do
		spawn(function()
			if v.Name == "Ta10" or v.Name == "Ta15" then
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
				wait(0.001)
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 5
				v.Head.Lens.Transparency = .5
				v.Head.Beam.light.Transparency = NumberSequence.new(.5)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 7.5
				v.Head.Lens.Transparency = .25
				v.Head.Beam.light.Transparency = NumberSequence.new(.25)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 10
				v.Head.Lens.Transparency = 0
				v.Head.Beam.light.Transparency = NumberSequence.new(0)
			end
		end)
	end
	wait(0.01)
	for i,v in pairs (game:GetService("Workspace").TaLights:GetChildren()) do
		spawn(function()
			if v.Name == "Ta9" or v.Name == "Ta16" then
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
				wait(0.001)
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 5
				v.Head.Lens.Transparency = .5
				v.Head.Beam.light.Transparency = NumberSequence.new(.5)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 7.5
				v.Head.Lens.Transparency = .25
				v.Head.Beam.light.Transparency = NumberSequence.new(.25)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 10
				v.Head.Lens.Transparency = 0
				v.Head.Beam.light.Transparency = NumberSequence.new(0)
			end
		end)
	end
	for i,v in pairs (game:GetService("Workspace").TaLights:GetChildren()) do
		spawn(function()
			if v.Name == "Ta8" or v.Name == "Ta17" then
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
				wait(0.001)
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 5
				v.Head.Lens.Transparency = .5
				v.Head.Beam.light.Transparency = NumberSequence.new(.5)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 7.5
				v.Head.Lens.Transparency = .25
				v.Head.Beam.light.Transparency = NumberSequence.new(.25)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 10
				v.Head.Lens.Transparency = 0
				v.Head.Beam.light.Transparency = NumberSequence.new(0)
			end
		end)
	end
	wait(0.01)
	for i,v in pairs (game:GetService("Workspace").TaLights:GetChildren()) do
		spawn(function()
			if v.Name == "Ta7" or v.Name == "Ta18" then
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
				wait(0.001)
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 5
				v.Head.Lens.Transparency = .5
				v.Head.Beam.light.Transparency = NumberSequence.new(.5)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 7.5
				v.Head.Lens.Transparency = .25
				v.Head.Beam.light.Transparency = NumberSequence.new(.25)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 10
				v.Head.Lens.Transparency = 0
				v.Head.Beam.light.Transparency = NumberSequence.new(0)
			end
		end)
	end
	for i,v in pairs (game:GetService("Workspace").TaLights:GetChildren()) do
		spawn(function()
			if v.Name == "Ta6" or v.Name == "Ta19" then
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
				wait(0.001)
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 5
				v.Head.Lens.Transparency = .5
				v.Head.Beam.light.Transparency = NumberSequence.new(.5)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 7.5
				v.Head.Lens.Transparency = .25
				v.Head.Beam.light.Transparency = NumberSequence.new(.25)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 10
				v.Head.Lens.Transparency = 0
				v.Head.Beam.light.Transparency = NumberSequence.new(0)
			end
		end)
	end
	wait(0.01)
	for i,v in pairs (game:GetService("Workspace").TaLights:GetChildren()) do
		spawn(function()
			if v.Name == "Ta5" or v.Name == "Ta20" then
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
				wait(0.001)
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 5
				v.Head.Lens.Transparency = .5
				v.Head.Beam.light.Transparency = NumberSequence.new(.5)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 7.5
				v.Head.Lens.Transparency = .25
				v.Head.Beam.light.Transparency = NumberSequence.new(.25)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 10
				v.Head.Lens.Transparency = 0
				v.Head.Beam.light.Transparency = NumberSequence.new(0)
			end
		end)
	end
	for i,v in pairs (game:GetService("Workspace").TaLights:GetChildren()) do
		spawn(function()
			if v.Name == "Ta4" or v.Name == "Ta21" then
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
				wait(0.001)
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 5
				v.Head.Lens.Transparency = .5
				v.Head.Beam.light.Transparency = NumberSequence.new(.5)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 7.5
				v.Head.Lens.Transparency = .25
				v.Head.Beam.light.Transparency = NumberSequence.new(.25)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 10
				v.Head.Lens.Transparency = 0
				v.Head.Beam.light.Transparency = NumberSequence.new(0)
			end
		end)
	end
	wait(0.01)
	for i,v in pairs (game:GetService("Workspace").TaLights:GetChildren()) do
		spawn(function()
			if v.Name == "Ta3" or v.Name == "Ta22" then
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
				wait(0.001)
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 5
				v.Head.Lens.Transparency = .5
				v.Head.Beam.light.Transparency = NumberSequence.new(.5)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 7.5
				v.Head.Lens.Transparency = .25
				v.Head.Beam.light.Transparency = NumberSequence.new(.25)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 10
				v.Head.Lens.Transparency = 0
				v.Head.Beam.light.Transparency = NumberSequence.new(0)
			end
		end)
	end
	for i,v in pairs (game:GetService("Workspace").TaLights:GetChildren()) do
		spawn(function()
			if v.Name == "Ta2" or v.Name == "Ta23" then
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
				wait(0.001)
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 5
				v.Head.Lens.Transparency = .5
				v.Head.Beam.light.Transparency = NumberSequence.new(.5)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 7.5
				v.Head.Lens.Transparency = .25
				v.Head.Beam.light.Transparency = NumberSequence.new(.25)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 10
				v.Head.Lens.Transparency = 0
				v.Head.Beam.light.Transparency = NumberSequence.new(0)
			end
		end)
	end
	wait(0.01)
	for i,v in pairs (game:GetService("Workspace").TaLights:GetChildren()) do
		spawn(function()
			if v.Name == "Ta1" or v.Name == "Ta24" then
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
				wait(0.001)
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 5
				v.Head.Lens.Transparency = .5
				v.Head.Beam.light.Transparency = NumberSequence.new(.5)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 7.5
				v.Head.Lens.Transparency = .25
				v.Head.Beam.light.Transparency = NumberSequence.new(.25)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 10
				v.Head.Lens.Transparency = 0
				v.Head.Beam.light.Transparency = NumberSequence.new(0)
			end
		end)
	end
	wait(0.01)
	for i,v in pairs (game:GetService("Workspace").TaLights:GetChildren()) do
		spawn(function()
			if v.Name == "Ta12" or v.Name == "Ta13" then
				v.Head.Beam.SpotLight.Brightness = 10
				v.Head.Lens.Transparency = 0
				v.Head.Beam.light.Transparency = NumberSequence.new(0)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 7.5
				v.Head.Lens.Transparency = .25
				v.Head.Beam.light.Transparency = NumberSequence.new(.25)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 5
				v.Head.Lens.Transparency = .5
				v.Head.Beam.light.Transparency = NumberSequence.new(.5)
				wait(0.001)
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
				wait(0.001)
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
			end
		end)
	end
	wait(0.01)
	for i,v in pairs (game:GetService("Workspace").TaLights:GetChildren()) do
		spawn(function()
			if v.Name == "Ta11" or v.Name == "Ta14" then
				v.Head.Beam.SpotLight.Brightness = 10
				v.Head.Lens.Transparency = 0
				v.Head.Beam.light.Transparency = NumberSequence.new(0)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 7.5
				v.Head.Lens.Transparency = .25
				v.Head.Beam.light.Transparency = NumberSequence.new(.25)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 5
				v.Head.Lens.Transparency = .5
				v.Head.Beam.light.Transparency = NumberSequence.new(.5)
				wait(0.001)
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
				wait(0.001)
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
			end
		end)
	end
	for i,v in pairs (game:GetService("Workspace").TaLights:GetChildren()) do
		spawn(function()
			if v.Name == "Ta10" or v.Name == "Ta15" then
				v.Head.Beam.SpotLight.Brightness = 10
				v.Head.Lens.Transparency = 0
				v.Head.Beam.light.Transparency = NumberSequence.new(0)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 7.5
				v.Head.Lens.Transparency = .25
				v.Head.Beam.light.Transparency = NumberSequence.new(.25)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 5
				v.Head.Lens.Transparency = .5
				v.Head.Beam.light.Transparency = NumberSequence.new(.5)
				wait(0.001)
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
				wait(0.001)
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
			end
		end)
	end
	wait(0.01)
	for i,v in pairs (game:GetService("Workspace").TaLights:GetChildren()) do
		spawn(function()
			if v.Name == "Ta9" or v.Name == "Ta16" then
				v.Head.Beam.SpotLight.Brightness = 10
				v.Head.Lens.Transparency = 0
				v.Head.Beam.light.Transparency = NumberSequence.new(0)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 7.5
				v.Head.Lens.Transparency = .25
				v.Head.Beam.light.Transparency = NumberSequence.new(.25)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 5
				v.Head.Lens.Transparency = .5
				v.Head.Beam.light.Transparency = NumberSequence.new(.5)
				wait(0.001)
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
				wait(0.001)
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
			end
		end)
	end
	for i,v in pairs (game:GetService("Workspace").TaLights:GetChildren()) do
		spawn(function()
			if v.Name == "Ta8" or v.Name == "Ta17" then
				v.Head.Beam.SpotLight.Brightness = 10
				v.Head.Lens.Transparency = 0
				v.Head.Beam.light.Transparency = NumberSequence.new(0)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 7.5
				v.Head.Lens.Transparency = .25
				v.Head.Beam.light.Transparency = NumberSequence.new(.25)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 5
				v.Head.Lens.Transparency = .5
				v.Head.Beam.light.Transparency = NumberSequence.new(.5)
				wait(0.001)
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
				wait(0.001)
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
			end
		end)
	end
	wait(0.01)
	for i,v in pairs (game:GetService("Workspace").TaLights:GetChildren()) do
		spawn(function()
			if v.Name == "Ta7" or v.Name == "Ta18" then
				v.Head.Beam.SpotLight.Brightness = 10
				v.Head.Lens.Transparency = 0
				v.Head.Beam.light.Transparency = NumberSequence.new(0)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 7.5
				v.Head.Lens.Transparency = .25
				v.Head.Beam.light.Transparency = NumberSequence.new(.25)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 5
				v.Head.Lens.Transparency = .5
				v.Head.Beam.light.Transparency = NumberSequence.new(.5)
				wait(0.001)
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
				wait(0.001)
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
			end
		end)
	end
	for i,v in pairs (game:GetService("Workspace").TaLights:GetChildren()) do
		spawn(function()
			if v.Name == "Ta6" or v.Name == "Ta19" then
				v.Head.Beam.SpotLight.Brightness = 10
				v.Head.Lens.Transparency = 0
				v.Head.Beam.light.Transparency = NumberSequence.new(0)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 7.5
				v.Head.Lens.Transparency = .25
				v.Head.Beam.light.Transparency = NumberSequence.new(.25)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 5
				v.Head.Lens.Transparency = .5
				v.Head.Beam.light.Transparency = NumberSequence.new(.5)
				wait(0.001)
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
				wait(0.001)
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
			end
		end)
	end
	wait(0.01)
	for i,v in pairs (game:GetService("Workspace").TaLights:GetChildren()) do
		spawn(function()
			if v.Name == "Ta5" or v.Name == "Ta20" then
				v.Head.Beam.SpotLight.Brightness = 10
				v.Head.Lens.Transparency = 0
				v.Head.Beam.light.Transparency = NumberSequence.new(0)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 7.5
				v.Head.Lens.Transparency = .25
				v.Head.Beam.light.Transparency = NumberSequence.new(.25)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 5
				v.Head.Lens.Transparency = .5
				v.Head.Beam.light.Transparency = NumberSequence.new(.5)
				wait(0.001)
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
				wait(0.001)
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
			end
		end)
	end
	for i,v in pairs (game:GetService("Workspace").TaLights:GetChildren()) do
		spawn(function()
			if v.Name == "Ta4" or v.Name == "Ta21" then
				v.Head.Beam.SpotLight.Brightness = 10
				v.Head.Lens.Transparency = 0
				v.Head.Beam.light.Transparency = NumberSequence.new(0)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 7.5
				v.Head.Lens.Transparency = .25
				v.Head.Beam.light.Transparency = NumberSequence.new(.25)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 5
				v.Head.Lens.Transparency = .5
				v.Head.Beam.light.Transparency = NumberSequence.new(.5)
				wait(0.001)
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
				wait(0.001)
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
			end
		end)
	end
	wait(0.01)
	for i,v in pairs (game:GetService("Workspace").TaLights:GetChildren()) do
		spawn(function()
			if v.Name == "Ta3" or v.Name == "Ta22" then
				v.Head.Beam.SpotLight.Brightness = 10
				v.Head.Lens.Transparency = 0
				v.Head.Beam.light.Transparency = NumberSequence.new(0)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 7.5
				v.Head.Lens.Transparency = .25
				v.Head.Beam.light.Transparency = NumberSequence.new(.25)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 5
				v.Head.Lens.Transparency = .5
				v.Head.Beam.light.Transparency = NumberSequence.new(.5)
				wait(0.001)
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
				wait(0.001)
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
			end
		end)
	end
	for i,v in pairs (game:GetService("Workspace").TaLights:GetChildren()) do
		spawn(function()
			if v.Name == "Ta2" or v.Name == "Ta23" then
				v.Head.Beam.SpotLight.Brightness = 10
				v.Head.Lens.Transparency = 0
				v.Head.Beam.light.Transparency = NumberSequence.new(0)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 7.5
				v.Head.Lens.Transparency = .25
				v.Head.Beam.light.Transparency = NumberSequence.new(.25)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 5
				v.Head.Lens.Transparency = .5
				v.Head.Beam.light.Transparency = NumberSequence.new(.5)
				wait(0.001)
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
				wait(0.001)
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
			end
		end)
	end
	wait(0.01)
	for i,v in pairs (game:GetService("Workspace").TaLights:GetChildren()) do
		spawn(function()
			if v.Name == "Ta1" or v.Name == "Ta24" then
				v.Head.Beam.SpotLight.Brightness = 10
				v.Head.Lens.Transparency = 0
				v.Head.Beam.light.Transparency = NumberSequence.new(0)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 7.5
				v.Head.Lens.Transparency = .25
				v.Head.Beam.light.Transparency = NumberSequence.new(.25)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 5
				v.Head.Lens.Transparency = .5
				v.Head.Beam.light.Transparency = NumberSequence.new(.5)
				wait(0.001)
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
				wait(0.001)
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
			end
		end)
	end
end

From line 309, the _G.EFFECT_DIMMER things start from the buttom.
Basically:

Anything before line 309:

v.Head.Lens.Transparency = _G.EFFECT_DIMMER
v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
wait(0.001)
v.Head.Lens.Transparency = _G.EFFECT_DIMMER
v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
wait(0.001)
v.Head.Beam.SpotLight.Brightness = 5
v.Head.Lens.Transparency = .5
v.Head.Beam.light.Transparency = NumberSequence.new(.5)
wait(0.001)
v.Head.Beam.SpotLight.Brightness = 7.5
v.Head.Lens.Transparency = .25
v.Head.Beam.light.Transparency = NumberSequence.new(.25)
wait(0.001)
v.Head.Beam.SpotLight.Brightness = 10
v.Head.Lens.Transparency = 0
v.Head.Beam.light.Transparency = NumberSequence.new(0)

Everything after line 309:

v.Head.Beam.SpotLight.Brightness = 10
v.Head.Lens.Transparency = 0
v.Head.Beam.light.Transparency = NumberSequence.new(0)
wait(0.001)
v.Head.Beam.SpotLight.Brightness = 7.5
v.Head.Lens.Transparency = .25
v.Head.Beam.light.Transparency = NumberSequence.new(.25)
wait(0.001)
v.Head.Beam.SpotLight.Brightness = 5
v.Head.Lens.Transparency = .5
v.Head.Beam.light.Transparency = NumberSequence.new(.5)
wait(0.001)
v.Head.Lens.Transparency = _G.EFFECT_DIMMER
v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
wait(0.001)
v.Head.Lens.Transparency = _G.EFFECT_DIMMER
v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER

See the difference?

But yeah, If anyone could tell me how I would optimize this code and lower it down to 100 lines or less, that would be brilliant.

1 Like

you should look into for loops
https://education.roblox.com/en-us/resources/repeating-tasks-with-for-loops

What about the GetChildren())'s?

I’ve heard that you can use one for each other thing…

You are runnning 600 lines of code in a while loop, you should look into events.

You may want to use the TweenService for what you’re trying to do.
Also, wait() can only pause scripts for at least 0.03 seconds, so you’re script is quite a bit slower than you want.

1 Like

What does it do? You might benefit from using a function or two here? Why is it broken in to these named sections and sequenced in order? “if v.Name == “Ta11” or v.Name == “Ta14” then”

Is it some sort of light show?

Just ran across this. Many of these lines are the same as the 61* line code (down from 300-500?, per the OP) we are working on in the other thread. For anyone looking at this with a fresh set of eyes, refer to here:

Code not working as intended - Help and Feedback / Scripting Support - DevForum | Roblox

That one is down to 49 lines.

This code uses the same fade-in and fade-out methods.

1 Like

Correct, it is for light shows.

While this is technically true, it is true in the least meaningful way possible. Script performance does not degrade with a longer script (unless you write inefficient code). Network will be impacted minimally, as internally Roblox uses signed bytecode which will only take up a few more bytes of network recieve. If the script is server-sided, there will be no network impact.

Optimize for readability first.

I would also suggest not using _G, as there is often little reason to do so.

1 Like

Reason I am using _G is to match with a Fader that’s used for the code.

Hey, this might work. You can use a for loop and use ‘i’ to dynamically grab each object from the workspace. The second for loop using ‘j’ will grab the 2 objects you’re needing on each pass. Hope this helps.

local v

while wait(0.05) do
	for i = 1, 12 do
		for j = 1, 2 do
			if j == 1
				-- Count down from 12 to 1
				v = game:GetService("Workspace").TaLights:FindFirstChild("Ta" .. (12 - i + 1))
			else
				--Count up from 13 to 24
				v = game:GetService("Workspace").TaLights:FindFirstChild("Ta" .. (13 + i - 1))
			end

			v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
				wait(0.001)
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 5
				v.Head.Lens.Transparency = .5
				v.Head.Beam.light.Transparency = NumberSequence.new(.5)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 7.5
				v.Head.Lens.Transparency = .25
				v.Head.Beam.light.Transparency = NumberSequence.new(.25)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 10
				v.Head.Lens.Transparency = 0
				v.Head.Beam.light.Transparency = NumberSequence.new(0)
		end

		-- Wait every 4 items
		if i % 2 == 0 then
			wait(.01)
		end
	end

	wait(.01)

	for i = 1, 12 do
		for j = 1, 2 do
			if j == 1
				-- Count down from 12 to 1
				v = game:GetService("Workspace").TaLights:FindFirstChild("Ta" .. (12 - i + 1))
			else
				--Count up from 13 to 24
				v = game:GetService("Workspace").TaLights:FindFirstChild("Ta" .. (13 + i - 1))
			end

			v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
				wait(0.001)
				v.Head.Lens.Transparency = _G.EFFECT_DIMMER
				v.Head.Beam.light.Transparency = NumberSequence.new(_G.EFFECT_DIMMER)
				v.Head.Beam.SpotLight.Brightness = _G.EFFECT_DIMMER
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 5
				v.Head.Lens.Transparency = .5
				v.Head.Beam.light.Transparency = NumberSequence.new(.5)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 7.5
				v.Head.Lens.Transparency = .25
				v.Head.Beam.light.Transparency = NumberSequence.new(.25)
				wait(0.001)
				v.Head.Beam.SpotLight.Brightness = 10
				v.Head.Lens.Transparency = 0
				v.Head.Beam.light.Transparency = NumberSequence.new(0)
		end

		-- Wait every 4 items
		if i % 2 == 0 then
			wait(.01)
		end
	end
end

might want to check the link that golgi talked about

There were two big issues with the code you linked, one was optimization which we have not fixed and the other WET code, which we have. I took all the patterns in your code and tried to condense them so rather than repeating the same patter explicity we create a ruleset for the code to work by.

The largest optimization improvements you can make (which I didn’t) are two-fold:

  1. Don’t set properties constantly
    You have five values you switch between constantly, it’s much faster to create a cache of the part with different properties already set then CFraming rather than setting values constantly.
  2. Creating too many threads
  • 49 lines (untested, may require small changes).
-- Rather than have many copies of the same code with minor changes,
-- We've created a list of values to cycle through.

-- We cycle through them forwards and backwards depending on what 'round' they're on
local TRANSPARENCY = {_G.EFFECT_DIMMER, _G.EFFECT_DIMMER, .5, .25, 0}
local SPOTLIGHT_BRIGHTNESS = {_G.EFFECT_DIMMER, _G.EFFECT_DIMMER, 5, 7.5, 10}
local LIGHT_TRANSPARENCY = {NumberSequence.new(_G.EFFECT_DIMMER), NumberSequence.new(_G.EFFECT_DIMMER), NumberSequence.new(.5), NumberSequence.new(.25), NumberSequence.new(0)}

local function setValues(v, increment)
    local start, last = 1, 5

    start = increment < 0 and last
    last = increment < 0 and start

    for index = start, last, increment do
        v.Head.Lens.Transparency = TRANSPARENCY[index]
        v.Head.Beam.light.Transparency = LIGHT_TRANSPARENCY[index]
        v.Head.Beam.SpotLight.Brightness = SPOTLIGHT_BRIGHTNESS[index]
        task.wait(0.001)
    end
end

local TalLights = game:GetService("Workspace").TaLights:GetChildren()
local LIGHT_NAME = "Ta"

-- all waits and spawns have been switched to their newer counterparts
while true do -- replaced task.wait with true as it's bad practise to use while wait() loops.
    for round = 1, -1, -1 do
        if round == 0 then continue end
        local ta1, ta2 = 12, 13

        -- You had 12 copies of the same code so we loop through it instead
        for run = 1, 12 do
            local waitDelay = (run == math.floor(run) and 0.01) or 0 -- rules for when to delay, every second unless first or last 'run'
            if waitDelay == 1 or waitDelay == 12 then
                waitDelay = 0.01
            end

            for _, light in ipairs(TalLights) do
                if light.Name ~= LIGHT_NAME..tostring(ta1) and light.Name ~= LIGHT_NAME..tostring(ta2) then
                    return -- check numbers appended to part name rather than string checks, this allows us to modify the number
                end
                task.defer(setValues, light, round)
            end

            ta1 -= 1 -- increase and decrease number which we check against part name
            ta2 += 1
            task.wait(waitDelay)
        end
        task.wait(round == 1 and 0.01 or 0)
    end
    task.wait(0.05)
end

This would be a good example of when to use OOP. Each one seems to have the same kinds of things, so if you were to store them as an object, you’d be able to access them a lot easier and separate them into groups without needing to have a long meaty code. Then for the object, you can have specific functions that do exactly what you need it to do, and just call that function each time to automatically set it up.

It’ll make it easier to read, and much easier to organize and edit.

Several things here.
First of all, wait(0.01) and wait(0.001) are never going to behave any differently than wait() since any call to wait will only ever wait at least two frames, never less. Specifying a value does not make it more robust, or faster, so don’t specify very small numbers, just use wait() by itself.

Additionally, I would recommend using the newer task.wait and task.spawn features, because wait and spawn both have bad throttling. task.wait is also faster too, because it waits one frame instead of two. It solves the throttling issue in the same way that developers have been for ages, by using what is essentially equivalent to RunService.Heartbeat:Wait(), and since it doesn’t have throttling, task.wait(1) will always wait at most 1 second and up to 1 additional frame.

Also, your code can very easily be broken down into a single function or two. Almost all of your code is repeated many many times, and you’re spawning many of these loops over top of eachother, instead of simply just using a wait.

You do not need to be spawning anything here, and you don’t need that many loops, you only need one loop, and you can make helper functions to replace any repeated code so it isn’t repeated so many times.

Small correction, wait() doesn’t wait for at least two frames, it waits for at least 0.03 seconds.

After talking on Discord, I’m fairly sure @BenSBk is actually correct.

This is not actually technically correct, wait() actually waits for at least two frames, which is how the mechanism works internally.

The 0.03 number you are referring to is actually 0.033... aka 1/30. This number comes from the fact that wait() waits for two frames, and the default FPS cap is 60 FPS, meaning one frame takes 1/60th of a second. That means that two frames would take 2/60ths of a second, which is equivalent to 1/30th of a second, which, is where this number comes from.

No, wait() yields for at least 0.03 seconds, not two frames or 0.033… seconds. It’s just that at 60 FPS this lines up to be two frames, which is 0.033… seconds. This is apparent through an FPS unlocker:

(See above)

FPS unlockers do not unlock the true physics rate, which remains capped at 60 FPS. The wait time is measurably not 0.03 nor is there any hard coded value beyond the FPS cap, so this value of 0.03 doesn’t make any sense.

The time measured below is what the engine stats refer to as Physics, which is the rate of the physics engine, is what wait() relies on, and measurably isn’t the same as the Heartbeat event:
image

What you are printing is inaccurate and can be shown by measuring via os.clock(). Are you printing the value returned by wait, or the actual elapsed time here? I suspect it’s probably the prior, and the return value of wait is not particularly very accurate.

task.wait:

while true do
	local t = os.clock()
	task.wait()
	task.wait()
	print(os.clock() - t, 1/(os.clock() - t)) -- measured elapsed time and FPS
end

image

With FPS unlocker (is affected):
image

vs

Heartbeat:Wait()

local RunService = game:GetService("RunService")
while true do
	local t = os.clock()
	RunService.Heartbeat:Wait()
	RunService.Heartbeat:Wait()
	print(os.clock() - t, 1/(os.clock() - t)) -- measured elapsed time and FPS
end

image
With FPS unlocker (is affected):
image

vs

while true do
	local t = os.clock()
	wait()
	print(os.clock() - t, 1/(os.clock() - t)) -- measured elapsed time and FPS
end

image

With FPS unlocker (is not affected, because its tied to the physics rate):
image