An inquiry about day/night scripts

Hello,
I’m currently facing a sort of simple yet complex issue… I would like to increment TimeOfDay by 1 second (contrary to most Day/Night scripts which increment directly minutes) every… second.

I have so far came up with the following script:

local minutesAfterMidnight = 0
while true do
	minutesAfterMidnight = minutesAfterMidnight + .01 -- Hacky way to try to increment TimeOfDay by 1 second
	game.Lighting:SetMinutesAfterMidnight(minutesAfterMidnight)
	wait(.65) 
end

However, it randomly skips over sometimes, so far I tried modifying the wait()'s delay but to no avail, I’m still confused as to why it keeps skipping over randomly… I need help on that matter.

Regards,

3 Likes

The following code should work just fine:

local lightingService = game:GetService("Lighting")
local minutesAfterMidnight = 0

while true do
    wait(1)
    minutesAfterMidnight = minutesAfterMidnight + 1/60
    lightingService:SetMinutesAfterMidnight(minutesAfterMidnight)
end

The skipping may come from the small number we’re incrementing the minutes after midnight by, but the difference is so small that the skip shouldn’t be noticeable.

7 Likes

Yeah it’s a bit better, even if there’s a small skip (but as you said it’s not very noticeable), it’ll do the trick I guess :slight_smile:

1 Like

Not sure that is the best option because of those jumps. Try something like this:

--[[
Day/Night Cycle Handler
  ]]
--Directories/Services
local LightingService = game:GetService("Lighting")
local TweenService = game:GetService("TweenService")
local minutesAfterMidnight = Instance.new("NumberValue")
--Configure Value
minutesAfterMidnight.Value = 0
local TweenTo = TweenService:Create(minutesAfterMidnight,tweeninfo.new(86400,Enum.EasingStyle.Linear), {Value = 1440})
--Run the loop
while true do
   local Playing = TweenTo:Play()
   repeat wait() until TweenTo.Completed() --Once the event fires, restart the tween
   minutesAfterMidnight.Value = 0 --Reset the value to start again
end

Tweening is generally a more efficient strategy then waiting every second and having the script fire. Also apologies if I messed something up, I was hurrying along and thought I would reply.

You could probably improve upon the proposed solution in this mannerism:

local Lighting = game:GetService("Lighting")
local MinutesAfterMidnight = Lighting:GetMinutesAfterMidnight() -- Saves current time
local Increment = 1/60

while true do
    Lighting:SetMinutesAfterMidnight(MinutesAfterMidnight + Increment)
    MinutesAfterMidnight = Lighting:GetMinutesAfterMidnight()
    wait(1) -- Or whatever you choose
end

This script preserves the default time from Lighting that the server starts on, rather than pushing it back to 0. Can’t think of any reasonable benefit other than that though.

Interesting… I’ve never used tweens actually (bear in mind I’ve been a bit out of the loop [no pun intended] when it comes down to scripting), I’ll check if that could solve that skipping problem…

This works a little different than what your code does, but bare with me here:

--Services
local Lighting = game:GetService("Lighting")
local RunService = game:GetService("RunService")

--Variables
local secondsPerSecond = 1
local shiftIncrement = secondsPerSecond/3600
local startingClockTime = Lighting.ClockTime
local s = 0

--Function that will be our loop
local function LoopStep(step)
	
	local fullIncrement = shiftIncrement*s
	--Adds our entire increment to starting time and modulo to keep it between 0 and 24
	local nowClock = (startingClockTime + fullIncrement)%24
	--Apply that to clock time
	Lighting.ClockTime = nowClock
	
	s = s + step
end

--Don't have to worry about a while loop here
RunService.Heartbeat:Connect(LoopStep)

So what’s going on here is we have an event bouncing each time RunService’s heartbeat happens (around 1/60 of a second). It also doesn’t yield the thread. The argument it gives “step” is the amount of time since the last heartbeat (keep that in mind for later).

Next we have the step function. The property itself is a range from 0 to 23.999 and cannot be anything above or below without resulting in a default value that we cannot work with. So what I did was I made a difference-in-time (the “s” variable that gets added by “step” over time) variable and multiplied that by the increment (secondsPerSecond/3600). This gives us the amount we should add to our starting value up to the current time.

Finally I added that to the starting clock time and modulo’d it by 24 so that it can stay within that 0 - 23.999 range, and that is how you get the clock time. :slight_smile:

Edit: here’s a video of some sped-up results:

2 Likes

Man thank you so much! That’s exactly what I was looking for :slight_smile:

2 Likes