Need help with a day night cycle script

I initially went into studio to implement this in a game i’ve been collaborating on with some other guys on the platform for fun intending it for it to maybe be a 10 minute job or less and ended up staring at the code with no actual idea how to implement this correctly. I wanted to create one that is easy on the eyes so that the other devs know what they’re dealing with (they don’t know how to code). I have a config property set up with some attributes and a manual script (redundant but why not) with the main code being this:

local light = game:GetService("Lighting")
local RunService = game:GetService("RunService")
local minutes = script.Config:GetAttribute("Minutes")
local startinghour = script.Config:GetAttribute("StartingHour")

local MinutesPerSecond = minutes
local timev = light:GetMinutesAfterMidnight()

local timev = startinghour * 60

while true do
	
	
	timev = timev + minutes

	wait(1)

end

the GetMinutesAfterMidnight function was referenced by this following code from another thread/post and now I realize I don’t really understand what it actually does.

local Lighting = game:GetService("Lighting")

local Minutes = 7 * 60

while task.wait() do
	Lighting:SetMinutesAfterMidnight(Minutes)
	Minutes = Lighting:GetMinutesAfterMidnight()

	if Minutes > 19 * 60 then
		Minutes = Minutes + 3
	elseif Minutes > 7 * 60 then
		Minutes = Minutes + 1
	else
		Minutes = Minutes + 3
	end
end
1 Like

What is exactly your question? does your script works? or you wanna know how minutesAfterMidnight works?

To have a very simple script and using RunService instead of a while loop, you could use this:

local Lighting = game:GetService("Lighting")

local startTime = 12 -- Your config value, script.Config:GetAttribute("StartTime")
local speed = 0.5 -- your config speed, script.Config:GetAttribute("Speed")

local progress = startTime * 60

game:GetService("RunService").Heartbeat:Connect(function()
	progress = progress + speed
	Lighting:SetMinutesAfterMidnight(progress)
	progress = Lighting:GetMinutesAfterMidnight()
end)

GetMinutesAfterMidnight as it says returns the number of minutes after midnight or 12am. So if it is 1am it returns 60(1*60), if it is 8am it returns 480(8*60). The first script you have won’t do anything because it doesn’t actually call SetMinutesAfterMidnight it just loops changing a variable called timev. If you did add a SetMinutesAfterMidnight(timev) it would increase by the Minutes value in your config file every second. So if your file had a 1 for Minutes every second would be a game minute and so your full 24 hour roblox day would last for 24 minutes.

The second script will execute once every heartbeat which is variable and if the time is > 7pm or <= 7am it will add 3 minutes for every heartbeat and 1 minute if the time is > 7am and <= 7pm thereby making nighttime much quicker and actually your roblox day would happen super fast.

I’d suggest maybe changing task.wait() to task.wait(1) in the second script and then creating config values for day and night and use those variables instead of Minutes+3 or Minutes+1 and you can set those 2 values how you like it so your day cycle is as long as you want and your night cycle is how you like it.

i just looked up the lighting documentation and tested the code on there to see if it worked for my use case and it does. Initially before making the post I tried making it so that the script referenced the config instance attributes to have the server on a set starting hour and tell how many seconds should pass before a minute in-game passes; the configuration and manual script detailing how it works was parented underneath the actual script with the code and I was simply trying to make the code work, which wasn’t doing anything. No errors, nothing.

I actually found the solution through the lighting documentation (admittedly I should’ve checked this first before requesting help on the forums). I made it more understandable by changing how the minute attribute works so that instead of the number representing how many seconds before a minute, it’s a second for how many minutes in-game. I’ll just list it here for anyone who comes across it wishes to use it:

local light = game:GetService("Lighting")
local RunService = game:GetService("RunService")
local timemultiplier = script.Config:GetAttribute("Minutes")
local startinghour = script.Config:GetAttribute("StartingHour")

local start = startinghour * 60
local waitTime = 60 / 60



while true do
	start = start + timemultiplier
	
	light:SetMinutesAfterMidnight(start)
	
	task.wait(waitTime)
end

image

2 Likes

Sounds great!
Doesnt seem like a more understandable but yup xD

local light = game:GetService("Lighting")
-- local RunService = game:GetService("RunService") -- Why having it if you not gonna use it?
local timemultiplier = script.Config:GetAttribute("Minutes")
local startinghour = script.Config:GetAttribute("StartingHour")

local start = startinghour * 60
local waitTime = 60 / 60 -- ?? why not just typing 1 ?

while true do
	start = start + timemultiplier -- This variable will increase over time without limit
-- I mean, on the first iteration will be 721, and it will arise to infinite
-- Thats why the script you did show from another post used Lighting:GetMinutesAfterMidnight() to "normalize" the number, theres not point on giving a 456489465489465489 number just to set a Time of day
	light:SetMinutesAfterMidnight(start)
	task.wait(waitTime) -- The while loop will not cause a smooth progression
end

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