Light changing not working?

Im trying to make this script that will change the light color of all lights in game.

Couldnt upload video because it was stuck uploading at 82%
Light color wont change at all when the alarm goes off.

Ive gone to scriptinghelpers.org but I got really no help there…

for i,v in pairs (workspace.Lights:GetDescendants()) do
	if v:IsA("SpotLight") and script.Parent.Activated.Value == true then
		while true do
			v.Color = Color3.fromRGB(255, 0, 0)
			wait(1)
			v.Color = Color3.fromRGB(255,255,255)
			wait(1)
			v.Color = Color3.fromRGB(0,0,0)
		end
	elseif script.Parent.Activated.Value == false then
		if v:IsA("SpotLight") then
			v.Color = Color3.fromRGB(255,255,255)
		end
	end
end	
2 Likes

Do only one of the lights flicker? You have a while loop that is going to run infinitely for the first light in the array that you iterating through. This means that the while loop for first descendant in that for loop will yield the rest of the loop and prevent it from moving onto other elements in that array.

You should prevent that while loop from yielding and also have a condition in order to break the loop when you no longer want the alarm to be active.

You have created a similar thread:

But it’s a bit of a change and I’ll cut you some slack. This is the part that is wrong:

while true do
	v.Color = Color3.fromRGB(255, 0, 0)
	wait(1)
	v.Color = Color3.fromRGB(255,255,255)
	wait(1)
	v.Color = Color3.fromRGB(0,0,0)
end

Add an extra wait, and coroutine and it should fix it:

coroutine.wrap(function() -- Coroutine
	while true do
		v.Color = Color3.fromRGB(255, 0, 0)
		wait(1)
		v.Color = Color3.fromRGB(255,255,255)
		wait(1)
		v.Color = Color3.fromRGB(0,0,0)
		wait(1) -- Extra wait
	end
end)()

A coroutine is basically a script inside of a script, this is useful because lua needs the previous line to finish running before executing the text line.

remove that from your code and it should work.

image
As you can see I only get 1 spotlight.

for i,v in pairs (workspace.Lights:GetDescendants()) do
	if v:IsA("SpotLight") and script.Parent.Activated.Value == true then
		coroutine.wrap(function()
			while true do
				print(v)
				v.Color = Color3.fromRGB(255, 0, 0)
				wait(1)
				v.Color = Color3.fromRGB(255,255,255)
				wait(1)
				v.Color = Color3.fromRGB(0,0,0)
				wait(1)
			end
		end)()
		break
	elseif script.Parent.Activated.Value == false then
		if v:IsA("SpotLight") then
			print(v)
			v.Color = Color3.fromRGB(255,255,255)
			break
		end
	end
end	

This is my current code. I added prints to debug. I dont get an error.

Your break statement runs almost immediately and will break the original for loop causing the entire loop to stop. Your implementation of both break statements is incorrect. A break statement works like this:

for i = 1,10 do
	print(i)
	if i == 5 then
		break
	end
end

It will run 5 times and print from 1 to 5, then break the loop once the variable i is equal to 5. This will stop the loop from running. The same principle applies to all loops in your code snippet. The break statement needs to be inside the loop, and your first break is stopping the original for loop, not the while loop.

1 Like
for i,v in pairs (workspace.Lights:GetDescendants()) do
	if v:IsA("SpotLight") and script.Parent.Activated.Value == true then
		coroutine.wrap(function()
			while true do
				v.Color = Color3.fromRGB(255, 0, 0)
				wait(1)
				v.Color = Color3.fromRGB(255,255,255)
				wait(1)
				v.Color = Color3.fromRGB(0,0,0)
				wait(1)
				print(v)
			end
		end)()
	elseif script.Parent.Activated.Value == false then
		if v:IsA("SpotLight") then
			v.Color = Color3.fromRGB(255,255,255)
			print(v)
		end
	end
end	

Its kind of strange that it prints but the light colors dont change… weird

The light colors probably don’t change due to script.Parent.Activated.Value not set to true. I assume Activated.Value is a BoolValue object?

I made quick fix, I added some small additions to the script. Please be aware, if you’re using this script alone in a LocalScript, it will not duplicate across all players. Use ServerScriptService instead. You may adjust this script to fulfill your needs:

local location = workspace.Lights --Lights you want to adjust
local bool = location.Activated --BoolValue within workspace.Lights
local seconds = 1 --Blink timer
local pattern = {{255,0,0}, {255,255,255}, {0,0,0}}
--You can put as much colors as you want in the array above

--START OF SCRIPT

local lights = location:GetDescendants()

local function startLights()
	for i,v in pairs(lights) do
		if v:IsA("SpotLight") and bool.Value then
			coroutine.wrap(function()
				while bool.Value do
					for i=1, #pattern do
						if not bool.Value then break end
						v.Color = Color3.fromRGB(pattern[i][1], pattern[i][2], pattern[i][3])
						wait(seconds)
					end
				end
				v.Color = Color3.fromRGB(255, 255, 255)
			end)()
		end
	end
end

startLights()
--If bool is true, it will blink, otherwise it would not.

bool.Changed:Connect(startLights)
--If bool changes state, it will call function automatically
1 Like