Making lights randomly flash, when value is set to true or false

Hey. So I’ve been working on script that checks certain value, and according to the value changes visual properties of all lights in certain folder.

while true do
    wait(2)
    local Blackout = game.Workspace.PowerInformationLights.Blackout.Value
    
    if Blackout == false then
            for i,e in pairs(workspace.FacilityLights.Bulbs:GetChildren()) do
            e.Material = 'Neon'
            e.Transparency = 0
            
        end
else
            for i,e in pairs(workspace.FacilityLights.Bulbs:GetChildren()) do
            e.Material = 'SmoothPlastic'
            e.Transparency = 0.8
            
        end
    end
end

The script itself works, but I was wondering, how can I make that when power is out / restored, all lights don’t immediately start shining, but they all start randomly flickering, and after while they turn on / off. Any idea how could I achieve that?

Also, I have separate script for actual SpotLights, I’ll just use this script there as well.

I have barely any Lua scripting knowledge, but I have experience with Python and JS so this is what I might do…

math.randomseed(tick()) -- makes it more random

function lightsOn()
    local BlackoutVal =  local Blackout = game.Workspace.PowerInformationLights.Blackout.Value
    local repetitions = math.floor( math.random(5, 15) ) -- number of times it will flicker
    local currentPlace = 0 -- place in the while loop
    local currentTimer = math.random(0.1, 1.5) -- time before lights turn off/on
    
    while currentPlace < repetitions do
        wait(currentTimer) -- wait for the set random amount of time
        BlackoutVal = true
        currentTimer = math.random(0.1, 1.5) -- makes a new random timer
        currentPlace = currentPlace + 1 -- moves up the while loop
    end

    BlackoutVal = true -- just in case the random flickering leaves it off
end

Just a sort of disclaimer, I’m not the best at Roblox scripting and this might not work. Tell me if there are any errors and I’ll see what’s causing it (probably a dumb syntax error)
Hope this helps!

That just broke the script, it doesn’t work.

hello try this

local hasBeenShutDown = false
local repeats = 20
local time_between_flick = 0.3

while true do
	wait(2)
	local Blackout = game.Workspace.PowerInformationLights.Blackout.Value

	if Blackout == false then
		if hasBeenShutDown == true then
			for i = 1,repeats,1 do
				local r = math.random(1,3)
				if r == 1 then
					for i,e in pairs(workspace.FacilityLights.Bulbs:GetChildren()) do
						e.Material = 'Neon'
						e.Transparency = 0


					end
				else
					for i,e in pairs(workspace.FacilityLights.Bulbs:GetChildren()) do
						e.Material = 'SmoothPlastic'
						e.Transparency = 0.8
						hasBeenShutDown = true

					end
				end
				wait(time_between_flick)
			end
			hasBeenShutDown = false
		else
			for i,e in pairs(workspace.FacilityLights.Bulbs:GetChildren()) do
				e.Material = 'Neon'
				e.Transparency = 0

		
			end
		end
		
	else
		for i,e in pairs(workspace.FacilityLights.Bulbs:GetChildren()) do
			e.Material = 'SmoothPlastic'
			e.Transparency = 0.8
			hasBeenShutDown = true

		end
	end
end

That also doesn’t work. Output doesn’t say any errors, but when I change the value, nothing happenes. No change of material, no change of transparency.

try to add prints to the code to debug, since i can’t replicate your game like printing the random number, if its prints the random number that means that’s the part with for i,e in pairsz wich doesn’t work

also doing while true is a bad practice since it’s infinite loop, use Instance | Documentation - Roblox Creator Hub

According to prints, script stops at phase when it checks hasBeenShutDown.

that means this part

else
		for i,e in pairs(workspace.FacilityLights.Bulbs:GetChildren()) do
			e.Material = 'SmoothPlastic'
			e.Transparency = 0.8
			hasBeenShutDown = true

		end
	end

is not good so it doens’t detect the value change

that means the script doens’t detect when the value change

Well, it doesn’t even chceck hasBeenShutDown. It stops on line 9. Line 10 isn’t executed.

It’s pretty simple, you are on the right path:

while true do
    wait(Max * 2) -- wait so the flickerings don't start to pile up.
    -- again, would recommend a different type of wait.
    local Blackout = game.Workspace.PowerInformationLights.Blackout.Value
    
    if Blackout == false then
        for i,e in pairs(workspace.FacilityLights.Bulbs:GetChildren()) do
            coroutine.wrap(function() -- create a coroutine so as to not yield the loop
                wait(math.random(Min, Max) -- wait a random amount of time
                -- psa: would use a custom wait, as the wait queue can get filled up quickly.
                e.Material = 'Neon'
                e.Transparency = 0
                wait(math.random(Min, Max)
                e.Material = 'SmoothPlastic' -- return to old state so it can flash again. Remove this and the wait above if you just want them all on when false, and just have them flicker on.
                e.Transparency = 0.3
            end)() -- run the coroutine
        end
    else
        for i,e in pairs(workspace.FacilityLights.Bulbs:GetChildren()) do
            e.Material = 'SmoothPlastic'
            e.Transparency = 0.3
        end
    end
end

This may not be what you want, but hopefully it gets you on the right path.

Unfortunately, that does not help as well. At this point I lost hope and I think I’ll make all lights shut down at same time :confused:

Any errors in the console? Are you actually running the function I made, because this won’t do anything if you just put it in a script. You have to run lightsOn() whenever the lights turn on for my function to be used.

I did that, don’t worry. But there are no errors in output.