How To Make A Day/Night Cycle!

So I recently found a new way to make a day/night cycle. It uses corountines, which I found is a better way to schedule it. Here’s the code if you want to try it yourself:

--Place this code in a script in ServerScriptService.
local Lighting = game:GetService("Lighting")

coroutine.resume(coroutine.create(function()
	while true do
		Lighting.ClockTime += 0.1
		task.wait(1)
	end
end)

Let me know if you have any questions!

15 Likes

there are many ways to create a daylight cycle, here is one

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

RunService.Heartbeat:Connect(function()
   Lighting.ClockTime += 0.1
   task.wait(1)
end)

read this part in “About the Community Tutorials category”

2 Likes

Yes, this would work. But it will look snappy to players, especially when they are laggy. You can see it in this video.

It would be better if the client did the day/night cycle. But this would make players out of sync, so you need to use workspace:GetServerTimeNow() for a time synced across all clients and the server. I made a new localscript to do this, and this is the result. The sun and moon are moving more smoothly, but shadows are still snappy, unfortunately. This is an issue I don’t think is possible to fix.

game:GetService("RunService").Heartbeat:Connect(function()
	game.Lighting.ClockTime = (workspace:GetServerTimeNow() / 60) % 24
end)
6 Likes

Yeah it looks pretty snappy
@Kittylitterking123 try this:

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

while true do
	task.wait(0.048)
	Lighting.ClockTime += 0.002
end

It works pretty well, the full 24 hour cycle lasts about 4-6 minutes (Estimate)

1 Like

That would be even more unreliable. I don’t think there’s a fix for the snapping.

1 Like

I’ve never even heard of GetServerTimeNow(). What exactly does it do?

I already said what it does. It’s basically tick() but it’s the same for everybody.

1 Like

That makes sense. Thank you. You also said to use the recommend code you gave in a local script. Where exactly is it parented to?

The code can be run anywhere as long as it is client sided. I recommend in StarterPlayerScripts, in a ScreenGui with ResetOnSpawn set to false, or ReplicatedFirst. What I would actually do is incorporate it in my modular system, but that’s complicated.

1 Like

Thank you. I’ll try that. By the way, what would your modular system look like?

2.5K VIEWS?! Thank you guys so much! Also, I just noticed that I did this with coroutines.

I’m not sure why as there is really no point, so you don’t have to do this. I think the reason why is because when I wrote that snippet, it was from a script of mine that already had a while true do loop or something and I wanted both of them to run simultaneously. I guess I could’ve just combined them, but for some reason I didn’t.

Let me know if there is something you guys want me to make a tutorial on! I would be happy to do so! Have a wonderful day! :slight_smile: