My script is looping for no reason

I was making a script that when you pull the lever down, all lights that are in the same folder will turn off/ on. However, for some reason it keeps looping and only turn off one at a time instead of all at the same time.

for _,lightfold in pairs(game.Workspace["Local Objects"].Lights:GetDescendants()) do
	if lightfold.Name == "Light Group" then

		--Switch Variables
		local switch = lightfold.Switch["Light Switch"]
		local handle = switch.Handle.Handle
		local prompt = handle.ProximityPrompt
		local pole = switch.Handle.Pole
		local degree = switch.Turn.Value
		local cooldown = switch.Cooldown.Value
		
		--Tween
		local tween = game:GetService("TweenService")
		
		local goaldown = {}
		goaldown.CFrame = pole.CFrame * CFrame.Angles(math.rad(degree), 0, 0)
		
		local goalup = {}
		goalup.CFrame = pole.CFrame * CFrame.Angles(0, 0, 0)
		
		local Info = TweenInfo.new(1)
		local Down = tween:Create(pole, Info, goaldown)
		local Up = tween:Create(pole, Info, goalup)
			
		--The Main Script
		prompt.Triggered:Connect(function()
			for _,lightmodel in pairs(lightfold:GetDescendants()) do
				if lightmodel:IsA'Model' and lightmodel.Name == "Light" then
					--Light Variables
					local light = lightmodel.Lights
					local glow = light.SpotLight
					
					--Turn it off or on
					if prompt.ActionText == "Turn Off" then
						Down:Play()
						glow.Enabled = false
						light.BrickColor = BrickColor.new("Dark stone grey")
						light.Material = "SmoothPlastic"
						prompt.Enabled = false
						print("The light is turned off")
						wait(cooldown)
						prompt.ActionText = "Turn On"
						prompt.Enabled = true
					elseif prompt.ActionText == "Turn On" then
						Up:Play()
						glow.Enabled = true
						light.BrickColor = BrickColor.new("Medium stone grey")
						light.Material = "Neon"
						prompt.Enabled = false
						print("The light is turned On")
						wait(cooldown)
						prompt.ActionText = "Turn Off"
						prompt.Enabled = true
					end
				end
			end
		end)
	end
end

This is a video showing what I mean, but you need to download it.
robloxapp-20220509-1824146.wmv (3.5 MB)

1 Like

one solution is to list all the options for tweeninfo, like:

TweenInfo.new(
1,
balh,
blah,
blah,
blah,
etc
)

but idk if its gonna fix it but it could

This won’t fix anything. :slight_smile: The rest of the tween-info is by default as it should be.

You’ve put a wait in your trigger-function, so the models will go off 1 by 1.

Here’s what you gotta do:

-- For each light folder, define a lever that turns on/off the lights.
-- When this lever's prompt is triggered, play the down-tween animation.
-- Now loop through all the lights in the folder and turn them off
-- Now put the wait(cooldown) outside of the loop you've done above
-- Now play the up-tween animation for the lever.
2 Likes

Could you possibly show what the script is like? I made something similar and am having the same problem and used your advice but it didn’t work.

Script:

function white(i)
	local impactsky = game.Workspace.skybox
	i.Color = Color3.fromRGB(0, 0, 0)
	i.Material = Enum.Material.Neon
	impactsky.Transparency = 0
	print("impact")
	wait(0.1)
end
function normal(i,orginalcolor,orginalmat)
	local impactsky = game.Workspace.skybox
	i.Color = orginalcolor
	i.Material = orginalmat
	impactsky.Transparency = 1
	print("stop")
end
rep.Events.impact.OnServerEvent:Connect(function()
	print("done")
	for _, i in pairs(game.Workspace:GetDescendants()) do
		print("did")
		if i.ClassName == "Part" then
			local orginalcolor = i.Color
			local orginalmat = i.Material
			white(i)
			normal(i,orginalcolor,orginalmat)
		end
	end
end)```