Flickering Streetlight

Hey there everyone! Another problem has surfaced whilst finishing up the scripting for our game, and It’s one that none of thr scripters in our group know what to do with.

Basically the plan is that we have a car crash that has struck a streetlight, and basically when the TimeOfDay passes 6PM, the streetlight has one of its lights flickering, until 6am when it turns off. The basic problem is that the light stays on (The default setting is on.), and doesn’t flicker.

I’ve tried putting in multiple edits to the script, but nothing seems to be working, so I’ve come here.

Here’s the script:

local dawn = game.Lighting:GetMinutesAfterMidnight() == 6 * 60
local dark = game.Lighting:GetMinutesAfterMidnight() == 18 * 60

if dawn then	-- checks for 6AM

	WorkingLightBulb.SpotLight.Enabled = false 
	WorkingLightBulb.SurfaceLight.Enabled = false -- Turns off the light.
end

if dark then		-- checks for 6PM
	
	while dark do
		
		wait(0.5)
		
		WorkingLightBulb.SpotLight.Enabled = false
		WorkingLightBulb.SurfaceLight.Enabled = false -- Flickers light to out
		
		wait(2)
		
		WorkingLightBulb.SpotLight.Enabled = true
		WorkingLightBulb.SurfaceLight.Enabled = true -- Flickers light to on
		
		wait(0.1)
		
		WorkingLightBulb.SpotLight.Enabled = false
		WorkingLightBulb.SurfaceLight.Enabled = false
		
		wait(0.3)
		
		WorkingLightBulb.SpotLight.Enabled = true
		WorkingLightBulb.SurfaceLight.Enabled = true
		
		end
end -- Done

If you know what’s going on here? Please contact me either here, or at Beast#5481 on Discord.

2 Likes

What’s not working? Could you explain what you mean by these lines of code?

game.Lighting:GetMinutesAfterMidnight() == 6 * 60
game.Lighting:GetMinutesAfterMidnight() == 18 * 60
1 Like

basically the top one states the time of Dawn, 6AM. The bottom one is Dusk, 6PM.

1 Like

Could you answer this question as well?

1 Like

Just edited above, the second paragraph states that when i Run the script, the light doesnt both flicker or switch off at Dawn/Switch on at Dusk.

1 Like

If the lights aren’t flickering after the set time, then you’re missing a loop to check if its dark or dawn. The script is only running once, and the dawn and dark variables never get updated.
You should add a loop to contain the current code that you have.

2 Likes

I’ve checked through the script, and nothing seems to be missing a loop of any sort.

Is the time in game always either exactly 6am or exactly 6pm? If it can be between then you need inequalities, not equalities.

1 Like

what would be an example of an inequality? Bc idek what they are xd
(Kinda new to scripting, bc I aint the scripter).

I’ll break this down as simple as I can.

  1. Everything in your script will only run once. Therefore, your variables will never update, meaning that dawn or dark will always be true, and the other the opposite.

  2. Your if statements only run once, meaning it will not keep running ‘if this and that then do this’. The solution to this, is to contain the if statements into a loop. Therefore, your if statements will be constantly checking if its dawn or dark.

  3. Your variables need to be updated, so redefine the variables every time the loop runs. A easy way to do this is to use a while true do loop.

so basically at the top put while true do, and at the bottom end. Right? Imao i sound like a toddler atm

1 Like

Yep! Another way to optimize your script would be to use math.random instead of placing intervals for the flickering manually.

wait so ur telling me this should work? Bc it isnt:

local WorkingLightBulb = game.Workspace.Islands.Strays.CarCrashIsland.Streetlights.BrokenStreetlight.Lights.WorkingLight
local dawn = game.Lighting:GetMinutesAfterMidnight() == 6 * 60
local dark = game.Lighting:GetMinutesAfterMidnight() == 18 * 60

while true do

if dawn then	-- checks for 6AM

	WorkingLightBulb.SpotLight.Enabled = false 
	WorkingLightBulb.SurfaceLight.Enabled = false -- Turns off the light.
end

if dark then		-- checks for 6PM
	
	wait(0.1)
	
	while dark do
		
		wait(0.5)
		
		WorkingLightBulb.SpotLight.Enabled = false
		WorkingLightBulb.SurfaceLight.Enabled = false -- Flickers light to out
		
		wait(2)
		
		WorkingLightBulb.SpotLight.Enabled = true
		WorkingLightBulb.SurfaceLight.Enabled = true -- Flickers light to on
		
		wait(0.1)
		
		WorkingLightBulb.SpotLight.Enabled = false
		WorkingLightBulb.SurfaceLight.Enabled = false
		
		wait(0.3)
		
		WorkingLightBulb.SpotLight.Enabled = true
		WorkingLightBulb.SurfaceLight.Enabled = true
		
		wait(0.1)
		
		end
	end -- Done
end

Not sure if you wanted something like this, and I am not a professional scripter as well.

local function flicker()
	wait(0.5)
	WorkingLightBulb.SpotLight.Enabled = false 
	WorkingLightBulb.SurfaceLight.Enabled = false
	wait(math.random(1,3))
	WorkingLightBulb.SpotLight.Enabled = true
	WorkingLightBulb.SurfaceLight.Enabled = true
end

if game.Lighting.ClockTime == 18 then
    repeat flicker() until game.Lighting.ClockTime == 6
end

if game.Lighting.ClockTime == 6 then
	WorkingLightBulb.SpotLight.Enabled = false 
	WorkingLightBulb.SurfaceLight.Enabled = false -- Turns the lights off since it's morning
end
1 Like

No, place the while loop at the top of your script, so that your variables will be redefined every time the loop runs. Put in other words, you will update wether or not dark or dawn is true.

An example script I wrote, with math.random included.

while true do
    wait(1)
    local function isDawn()
        return game.Lighting:GetMinutesAfterMidnight() == 6 * 60
    end

    if isDawn() then	-- this if function now checks if its dawn
        WorkingLightBulb.SpotLight.Enabled = false 
        WorkingLightBulb.SurfaceLight.Enabled = false -- Turns off the light.
    end

    if not isDawn() then		-- same as the if function at the top

    while not isDawn do
	    local randomTime = math.random(0.1,0.3)

	    wait(randomTime)
	
	    WorkingLightBulb.SpotLight.Enabled = not WorkingLightBulb.SpotLight.Enabled
	    WorkingLightBulb.SurfaceLight.Enabled = not  WorkingLightBulb.SurfaceLight.Enabled 
	end
end -- Done

@BEasTiO12 just redid the code, fixed some errors

Testing as u type, hopefully i can get this dumb one over with xd

Yeah still not working. Is it possibly a bug to do with Studio?

Equalities (==) are only true when both sides are exactly equal. So in your script it is only dawn when the time is perfectly 6am and only dusk when the time is perfectly 6pm, which means even just one minute either side causes you to miss the flicker logic.

Inequalities (<=, <, >, >=) capture a range, allowing this code to be run at any given moment and produce the correct output.

local Workspace = game:GetService( 'Workspace' )
local Lighting = game:GetService( 'Lighting' )

local WorkingLightBulb = Workspace.Islands.Strays.CarCrashIsland.Streetlights.BrokenStreetlight.Lights.WorkingLight

local minutes = Lighting:GetMinutesAfterMidnight()
local dark = minutes < 6*60 and minutes >= 18*60
local loopRunning = false

function updateLight()
    if not dark then
        -- switch it off
        WorkingLightBulb.SpotLight.Enabled = false 
        WorkingLightBulb.SurfaceLight.Enabled = false
    elseif not loopRunning then
        loopRunning = true
        while dark do
            wait(0.5)
		
		    WorkingLightBulb.SpotLight.Enabled = false
		    WorkingLightBulb.SurfaceLight.Enabled = false -- Flickers light to out
		
		    wait(2)
		
		    WorkingLightBulb.SpotLight.Enabled = true
		    WorkingLightBulb.SurfaceLight.Enabled = true -- Flickers light to on
		
		    wait(0.1)
		
		    WorkingLightBulb.SpotLight.Enabled = false
		    WorkingLightBulb.SurfaceLight.Enabled = false
		
		    wait(0.3)
		
		    WorkingLightBulb.SpotLight.Enabled = true
		    WorkingLightBulb.SurfaceLight.Enabled = true
        end
        loopRunning = false
    end
end

Lighting.Changed:Connect( function ()
    minutes = Lighting:GetMinutesAfterMidnight()
    dark = minutes < 6*60 or minutes >= 18*60
    updateLight()
end )
1 Like

This will flicker the entire day except for the 1 minute where it’s dawn. Depending on the day night cycle speed this brief pause in the flicker may even be imperceivable.

You’re also missing an “end” and will run into an error. Indenting your code consistently will help to identify missing ends.

1 Like