spawn(function()
while wait() do
-- Tween to midnight
local ToMidnight = TweenService:Create(Lighting, TweenInfo.new(60 * 5), {ClockTime = 0})
ToMidnight.Completed:Wait()
-- Tween to noon
local ToNoon = TweenService:Create(Lighting, TweenInfo.new(60 * 5), {ClockTime = 12})
ToNoon.Completed:Wait()
end
end)
At the moment, nothing happens. Script is definitely running tho. I’ve tried just doing
local Speed = 0.05
spawn(function()
while wait() do
Lighting:SetMinutesAfterMidnight(Lighting:GetMinutesAfterMidnight() + Speed)
end
end)
Which while yes, works, it makes the movement of the sun jittery, and thus the shadows don’t move smoothly, even with the speed being set really low. If I set the speed any lower, it’d take hours for a single day cycle to complete
Do not use wait().
Use RunService:BindToRenderStep() (or .Heartbeat:Wait()/:Connect())
If you want it to look the best on all machines, sync the time periodically with clients, and then the clients can manage the Day/Night cycle locally so that it is smooth.
This does not matter, the server can use server time to keep track of the time, and the client can ask for that time (or recieve it periodically) so that they can offset it themselves.
On client:
on every frame
minutes = minutes + deltaTime * speed
SetMinutesAfterMidnight(minutes)
Where the offset is given by the server as the current MinutesAfterMidnight
When you recieve it, you do
So if the servers time is 12:00:00 and stays at that on the server, and only the client changes the time, then let’s say client 1 is at 18:00:00, and now another players joins, they get given the server time of 12:00:00
First try one try to see if it really works, then you can add the second tween and third add the whole thing in a loop. Try step by step, don’t rush if it doesn’t work. So, I write from Mobile, but I would try it like this now:
local TweenService = game:GetService("TweenService")
game.Lighthing.ClockTime = 0
local Tween = TweenService:Create(game.Lighthing, TweenInfo.new(1.440), {ClockTime = game.Lighthing.ClockTime + 12})
while true do
Tween:Play()
Tween.Completed:Wait()
end
while wait() do
-- Tween to midnight
local ToMidnight = TweenService:Create(Lighting, TweenInfo.new(60 * 5), {ClockTime = 0})
ToMidnight.Completed:Wait()
-- Tween to noon
local ToNoon = TweenService:Create(Lighting, TweenInfo.new(60 * 5), {ClockTime = 12})
ToNoon.Completed:Wait()
end
You’re never actually :Play()ing the tween. Also while wait() do is bad practice.
Then @NinjoOnline you never defined Lighting, if you not have a variable with this then write game.Lighting, and not simply so Lighting. See above, hope my code will help
ClockTime uses 24 hour formatting if that helps. I personally just use something similar to your code snippet. But I use the return value (the delta time, aka the actual time waited) of wait to make it look more smooth.
local Lighting = game:GetService("Lighting")
local minutes = 0
while true do
Lighting:SetMinutesAfterMidnight(minutes)
minutes = minutes + wait(1) -- or whatever time
end
why the while wait() do idiom bad
In your case use
while true do
-- ...
wait(n)
It’s clearer.
It clearly indicates that the condition is true and that the loop is infinite because there is no terminating condition.
The while wait() do idiom only works because of a hack; a trick. It abuses the fact that wait returns a number, and numbers are truthy values in Lua. Which has led to tons of bad habits. Such as forgetting about the power of the conditional field of while loop.
while wait(n) do
if terminating_condition then
break
--[[
Don't get me wrong,
breaks aren't bad but using them over the conditional part isn't good. That's what it's for.
]]--
end
end
-- vs
while not terminating_condition do
wait(n)
end
-- So use the latter. It clearly indicates what the condition is.
And you are essentially throwing away that value which would have helped with the second code snippet you provided.
Finally, the idiom is misleading. It implies that the return value of wait is important, when really it isn’t.
There’s also that very unlikely chance that wait might be updated to return false or nil which will break your loops.