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)
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.
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)
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.
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!