How can i shorten my script to make it easy to edit

I made a loop script for my beams but i would like to shorten it to make it easy to edit can someone help with this?

Here is my script

PA = 0.1

while true do
	for i,v in pairs (game.Workspace.LLights:GetChildren())
	do
		if v.Name == "L1"
		then
	v.Head.Beam.light.Transparency = NumberSequence.new(0)
			v.Head.Beam.SpotLight.Enabled = true
			v.Head.Lens.Transparency = 0
		elseif v.Name == "L8"
		then
	v.Head.Beam.light.Transparency = NumberSequence.new(0)
			v.Head.Beam.SpotLight.Enabled = true
			v.Head.Lens.Transparency = 0
		end
	end
	wait(PA)
	for i,v in pairs (game.Workspace.LLights:GetChildren())
	do
		if v.Name == "L1"
		then
	v.Head.Beam.light.Transparency = NumberSequence.new(1)
			v.Head.Beam.SpotLight.Enabled = false
			v.Head.Lens.Transparency = 1
		elseif v.Name == "L8"
		then
	v.Head.Beam.light.Transparency = NumberSequence.new(1)
			v.Head.Beam.SpotLight.Enabled = false
			v.Head.Lens.Transparency = 1
		end
	end
	for i,v in pairs (game.Workspace.LLights:GetChildren())
	do
		if v.Name == "L2"
		then
	v.Head.Beam.light.Transparency = NumberSequence.new(0)
			v.Head.Beam.SpotLight.Enabled = true
			v.Head.Lens.Transparency = 0
		elseif v.Name == "L7"
		then
	v.Head.Beam.light.Transparency = NumberSequence.new(0)
			v.Head.Beam.SpotLight.Enabled = true
			v.Head.Lens.Transparency = 0
		end
	end
	wait(PA)
	for i,v in pairs (game.Workspace.LLights:GetChildren())
	do
		if v.Name == "L2"
		then
	v.Head.Beam.light.Transparency = NumberSequence.new(1)
			v.Head.Beam.SpotLight.Enabled = false
			v.Head.Lens.Transparency = 1
		elseif v.Name == "L7"
		then
	v.Head.Beam.light.Transparency = NumberSequence.new(1)
			v.Head.Beam.SpotLight.Enabled = false
			v.Head.Lens.Transparency = 1
		end
	end
	for i,v in pairs (game.Workspace.LLights:GetChildren())
	do
		if v.Name == "L3"
		then
	v.Head.Beam.light.Transparency = NumberSequence.new(0)
			v.Head.Beam.SpotLight.Enabled = true
			v.Head.Lens.Transparency = 0
		elseif v.Name == "L6"
		then
	v.Head.Beam.light.Transparency = NumberSequence.new(0)
			v.Head.Beam.SpotLight.Enabled = true
			v.Head.Lens.Transparency = 0
		end
	end
	wait(PA)
	for i,v in pairs (game.Workspace.LLights:GetChildren())
	do
		if v.Name == "L3"
		then
	v.Head.Beam.light.Transparency = NumberSequence.new(1)
			v.Head.Beam.SpotLight.Enabled = false
			v.Head.Lens.Transparency = 1
		elseif v.Name == "L6"
		then
	v.Head.Beam.light.Transparency = NumberSequence.new(1)
			v.Head.Beam.SpotLight.Enabled = false
			v.Head.Lens.Transparency = 1
		end
	end
	for i,v in pairs (game.Workspace.LLights:GetChildren())
	do
		if v.Name == "L4"
		then
	v.Head.Beam.light.Transparency = NumberSequence.new(0)
			v.Head.Beam.SpotLight.Enabled = true
			v.Head.Lens.Transparency = 0
		elseif v.Name == "L5"
		then
	v.Head.Beam.light.Transparency = NumberSequence.new(0)
			v.Head.Beam.SpotLight.Enabled = true
			v.Head.Lens.Transparency = 0
		end
	end
	wait(PA)
	for i,v in pairs (game.Workspace.LLights:GetChildren())
	do
		if v.Name == "L4"
		then
	v.Head.Beam.light.Transparency = NumberSequence.new(1)
			v.Head.Beam.SpotLight.Enabled = false
			v.Head.Lens.Transparency = 1
		elseif v.Name == "L5"
		then
	v.Head.Beam.light.Transparency = NumberSequence.new(1)
			v.Head.Beam.SpotLight.Enabled = false
			v.Head.Lens.Transparency = 1
		end
	end
end
1 Like

One thing that might help is to put the stuff that is being repeated into a function and you can just do that by doing the following…

local function updateSpotLight(beamName, spotLightStatus, beamTransparency)
	local lightRef = game.Workspace:WaitForChild("LLights"):WaitForChild(beamName)

	lightRef.Head.Beam.SpotLight.Enabled = spotLightStatus
	lightRef.Head.Lens.Transparency = NumberSequence.new(beamTransparency)
end

this will allow you to avoid having to write so those 2 lines of code in your script everytime you want to update a light, and you can just use a 5 line function. Hope that helps, Boston.

2 Likes

There are a lot of ways to go about the core of this, but you really want to get away from all those for loops.

local PA = 0.1
local llights = game.Workspace.LLights

local function setLightState(light, enabled)
	
	local head = light.head
	local beam = head.Beam
	local transparency = (enabled and 0 or 1)

	beam.light.Transparency = NumberSequence.new(transparency)
	beam.SpotLight.Enabled = enabled
	head.Lens.Transparency = transparency

end

local function flash(light1, light2)
	setLightState(light1, true)
	setLightState(light2, true)
	wait(PA)
	setLightState(light1, false)
	setLightState(light2, false)
end

while true do
	flash(llights.L1, llights.L8)
	flash(llights.L2, llights.L7)
	flash(llights.L3, llights.L6)
	flash(llights.L4, llights.L5)
end
1 Like