Day-Night Cycle Not Working

I want to have a day-night cycle where the day is 2 minutes and the night is 2 minutes as well.

The issue is that the script seems to not function properly and the day is a lot less than 2 minutes and the night is a lot more than 2 minutes.

Here’s the script.

local dayLength = 2

local cycleTime = dayLength*2
local minutesInADay = 2*2

local lighting = game:GetService("Lighting")

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

local timeRatio = minutesInADay / cycleTime

if dayLength == 1 then
	dayLength = 2
end

repeat
	local currentTime = tick()
	
	if currentTime > endTime then
		startTime = endTime
		endTime = startTime + cycleTime
	end
	
	lighting:setMinutesAfterMidnight((currentTime - startTime)*timeRatio)
	wait(1/15)
until false

Please, tell me where I went wrong.

1 Like
local dayLength = 2

local cycleTime = dayLength*2
local minutesInADay = 2*2

local lighting = game:GetService("Lighting")

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

local timeRatio = minutesInADay / cycleTime

if dayLength == 1 then
	dayLength = 2
end

repeat
	local currentTime = tick()
	
	if currentTime > endTime then
		startTime = endTime
		endTime = startTime + cycleTime
	end
	
	lighting:setMinutesAfterMidnight((currentTime - startTime)*timeRatio)
	wait(0.1) -- wait time of 1/15 seconds was very short and it was causing the issue.
until false

Also time goes backwards. Here’s is output of printing time of day after every update:
image

Wait so if suppose I want the daytime to be 2 minutes and night time to be 10 minutes what would the whole script look like (sorry for such a stupid question just really needed the answer to it)

A script for updating time without stopping could be something like this:

local lighting = game:GetService("Lighting")
while wait(x) do -- replace x with a number to wait x seconds before the next step, x number decides about amount of time to wait before the next time update
lighting:SetMinutesAfterMidnight(lighting:GetMinutesAfterMidnight() + y) -- replace y with amount of time to add to current time
print(lighting.TimeOfDay) -- printing TimeOfDay might help with finding good numbers for x and y
end

There is a plugin this is the plugin to do it (easier): Day And Night [WORKING] - Roblox

To make day longer than night or make night longer than day you can use similiar solution given my me, but you would have to use more loops.

There is a plugin this is the plugin to do it (easier): Day And Night [WORKING] - Roblox

Ok, so that’s for a game that never ends right??

I actually wanna know what values I have to change to make the day 2 minutes and the night 10 minutes in this script in this script (I’m extremely sorry that you’re being asked so many questions I’m kinda new to scripting

local dayLength = 2

local cycleTime = dayLength*2
local minutesInADay = 2*2

local lighting = game:GetService("Lighting")

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

local timeRatio = minutesInADay / cycleTime

if dayLength == 1 then
	dayLength = 2
end

repeat
	local currentTime = tick()
	
	if currentTime > endTime then
		startTime = endTime
		endTime = startTime + cycleTime
	end
	
	lighting:setMinutesAfterMidnight((currentTime - startTime)*timeRatio)
	wait(0.1)
until false

The plugin works, but how can I make night longer than day using this plugin?

– dayLength defines how long, in minutes, a day in your game is. Feel free to alter it.
local dayLength = 12

local cycleTime = dayLength12
local minutesInADay = 24
12

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

repeat
local currentTime = tick()

if currentTime > endTime then
	startTime = endTime
	endTime = startTime + cycleTime
end

lighting:setMinutesAfterMidnight((currentTime - startTime)*timeRatio)
wait(1/15)

until false

https://www.roblox.com/library/6814766695/Day-Night-Cycle

This works fine after fixing small errors, example: local cycleTime = dayLength12, but it’s supposed to be local cycleTime = dayLength. As I said it works.

-- dayLength defines how long, in minutes, a day in your game is. Feel free to alter it.
local dayLength = 12

local cycleTime = dayLength*12
local minutesInADay = 24*12

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

repeat
	local currentTime = tick()
	
	if currentTime > endTime then
		startTime = endTime
		endTime = startTime + cycleTime
	end
	
	lighting:setMinutesAfterMidnight((currentTime - startTime)*timeRatio)
	wait(1/15)
until false

Ok, so that’s for a game that never ends right??

I actually wanna know what values I have to change to make the day 2 minutes and the night 10 minutes in this script in this script (I’m extremely sorry that you’re being asked so many questions I’m kinda new to scripting

local dayLength = 2

local cycleTime = dayLength*2
local minutesInADay = 2*2

local lighting = game:GetService("Lighting")

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

local timeRatio = minutesInADay / cycleTime

if dayLength == 1 then
	dayLength = 2
end

repeat
	local currentTime = tick()
	
	if currentTime > endTime then
		startTime = endTime
		endTime = startTime + cycleTime
	end

Please, answer me to the question above

So never-ending day-night cycle is needed, day 2 minutes long and night 10 minutes long.

I don’t need a cycle for never ending game this cycle is only gonna run once but I want to know where the night time value is placed and where the day time value is placed.

This code has already configured that the day lasts 2 minutes and the night 10

-- dayLength defines how long, in minutes, a day in your game is. Feel free to alter it.
local dayLength = 2

local cycleTime = dayLength*10
local minutesInADay = 2*10

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

repeat
	local currentTime = tick()
	
	if currentTime > endTime then
		startTime = endTime
		endTime = startTime + cycleTime
	end
	
	lighting:setMinutesAfterMidnight((currentTime - startTime)*timeRatio)
	wait(1/15)
until false

I attempted making a simple script that can let you edit the night time value and the day time value by just keeping it in workspace

while true do

daytime = 10

nighttime = 10

local TweenService = game:GetService("TweenService")

local DayIn = TweenService:Create(game.Lighting, TweenInfo.new(daytime), {ClockTime = 12})

local DayOut = TweenService:Create(game.Lighting, TweenInfo.new(daytime), {ClockTime = 24})

DayIn:Play()

DayOut:Play()

wait(daytime)

local NightIn = TweenService:Create(game.Lighting, TweenInfo.new(daytime), {ClockTime = 0})

local NightOut = TweenService:Create(game.Lighting, TweenInfo.new(daytime), {ClockTime = 12})

NightIn:Play()

NightOut:Play()

wait(nighttime)

end

Wow, ty, much more simple than what I went for lol.

This code has already configured that the day lasts 2 minutes and the night 10
Fix bugs

-- dayLength defines how long, in minutes, a day in your game is. Feel free to alter it.
local dayLength = 2

local cycleTime = dayLength*10
local minutesInADay = 2*10

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

repeat
	local currentTime = tick()
	
	if currentTime > endTime then
		startTime = endTime
		endTime = startTime + cycleTime
	end
	
	lighting:setMinutesAfterMidnight((currentTime - startTime)*timeRatio)
	wait(1/15)
until false