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.
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.
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.
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.
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.
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
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
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 )
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.