Can you make a better optimization for my day/night script

Hello. Can you make a better optimization for my day/night script. So less ping or lag.

Script:

local lighting = game:GetService("Lighting")

local minutes = 840

while true do
    wait(0.25)
    lighting:SetMinutesAfterMidnight(minutes)
    minutes = minutes + 0.1
end

Nothing can really be optimized about this script. This script should not be creating lag perhaps it is something else in your game.

This belongs in #help-and-feedback:code-review, not scripting support.

@SnarlyZoo Is correct that your script will not cause lag. However, it can be improved in other ways.

  1. wait(0.25) will not always wait exactly 1/4 of a second. wait is an unstable function and doesn’t always do what you want.

  2. You don’t need to update lighting every fourth of a second anyways

Combining these two pieces of advice:

local lighting = game:GetService("Lighting")

local minutes = 840

local GAME_MIN_PER_REAL_MIN = 10 -- game time runs 10 times faster than real time

local DO_UPDATE_EVERY = 5 -- update lighting every 5 seconds (independent of above setting)

while true do
    lighting:SetMinutesAfterMidnight(minutes)

    local secondsActuallyWaited = wait(DO_UPDATE_EVERY)
    
    minutes += GAME_MIN_PER_REAL_MIN * secondsActuallyWaited / 60
end
1 Like

Thank you so much bro :slight_smile: