Detect Time Changed Event

Hi there,

I’m currently having a few issues detecting time changes through an event.

I’m currently trying to create a simple module that switches a street light on when the time is between an interval (ie when it’s nighttime) which I have working fine, except it uses timed polling.

I’ve tried using Lighting.Changed which unfortunately wasn’t firing when the time was changed through the console.
I’ve also tried using Lighting.LightingChanged which unfortunately wasn’t firing when the time was changed through the console.

I’ve had to resort to using:

function lightingChanged()
    local hoursAfterMidnight = math.floor(Lighting:GetMinutesAfterMidnight())
    if minimumHour >= hoursAfterMidnight or hoursAfterMidnight >= maximumHour then
        -- Switch on the light
    else
        -- Switch off the light
    end
end

while true do
    lightingChanged()
    wait(1)
end

This works exactly as intended, but I know that polling every second is pretty poor.
I was wondering whether there is an event I could connect to before running the function to try to optimise this?

Any help would be much appreciated.
Thanks :slight_smile:

I believe the function is already fine, and nope, there are no events that trigger when the night or day is reached. Also add some checks in that code that checks if lights are already on/off.

Use task.wait instead of wait, since this new method doesn’t throttle like wait does.
Also why don’t you just put the lightingChanged code in the loop itself. Since I see nothing else happening.

You could run the function every time ClockTime changes, but if you are tweening this property have in mind that it will fire every frame…

Ah thanks yeah forgot about the new task library!

The involvement of turning them on or off is simply changing a boolean value, checking whether the boolean value is or isn’t something else is redundant in my opinion.

Well I was hoping to connect the function to an event so I geared it up as such, the timed polling was simply what I had to do because I couldn’t get a connection to be made. Now that I have a connection, I’m able to connect the function for what I personally think is more readable and maintainable code.

You can use the universal Instance:GetPropertyChangedSignal(), which works exactly like a changed event. You can detect whenever the value of ClockTime (a float that varies between 0 and 24) changes, and switch the lights depending on the new time:

game.Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
    local newTime = game.Lighting.ClockTime
    if minimumHour >= newTime or newTime >= maximumHour then
        -- Switch on the light
    else
        -- Switch off the light
    end
end)

As focasds mentioned, this property does change frequently, but shouldn’t be an issue since you’re not doing anything with the signal.

3 Likes

Thank you!

For some reason I had disregarded this as an option because the Changed instance event wasn’t firing, which was stupid on my part.

1 Like

Thanks for bringing that to my attention! I had forgotten that was the case, so I edited my answer accordingly. Glad it was helpful!

1 Like