Day and Night Cycle

Hi all,

I’m currently using a very basic day and night cycle script for my game. However I wish to make it so the night passes 2x faster than the day. I’ve tried many things, but was unable to do that successfully. Usually it would completely break the sky and give me a seizure.

local dayLength = 15

local cycleTime = dayLength*60
local minutesInADay = 24*60

local lighting = game:GetService("Lighting")

local startTime = tick() - (lighting:getMinutesAfterMidnight() / minutesInADay)*cycleTime
local endTime = startTime + cycleTime

local timeRatio = minutesInADay / cycleTime

if dayLength == 0 then
	dayLength = 1
end

while task.wait(1/15) do
			local currentTime = tick()

			if currentTime > endTime then
				startTime = endTime
				endTime = startTime + cycleTime
			end
			lighting:SetMinutesAfterMidnight((currentTime - startTime)*timeRatio)
end

I want to make it so if the :GetMinutesAfterMidnight value is in between [0, 300] and [1200, 1439], the iterations would go 2x faster. How do I solve this issue?

Thanks in advance.

Why are you putting a runService.Heartbeat connection inside a while true do loop?

2 Likes

Wait I just tried it in studio ignore it

Sorry, I can’t use your code due to the fact that you’re making a Heartbeat connection inside a while loop, which would be really bad for the game’s optimization and performance. But I’ve already solved the problem by doing more research from existing threads in the forum.

local LightingService = game:GetService("Lighting")
local RunService = game:GetService("RunService")

RunService:BindToRenderStep("DayNightCycle", 1, function(dt)
	local ct = LightingService:GetMinutesAfterMidnight()
	local i =  (ct > 18*60 or ct < 6*60) and 10 or 5 
	LightingService:SetMinutesAfterMidnight(ct + dt * i)
end)
2 Likes

Makes sense, I was trying to make my day and night cycle compatable with what you wanted however the way mine is setup dosent allow for that sort of action without poor code managment, sorry

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.