Help with Day/Night cycle

I have a day/night cycle in my game, but I want day time to last for 5 minutes before turning night.
(I used a tutorial)

I don’t understand how to do that, I’ve played around with the Time and TimeChanged. But when I spawn into the game, it’s night and the moon just goes from the side of the sky to the other side.

(I don’t think that makes sense)

Basically when a player spawns into the game, I want it to be day time. I want day time and night to last 5 minutes before switching.

The animation is currently not smooth, and the sun goes from one side of the sky to the other. I want the animation to be smooth.

2 Likes

I would recommend using tweening to tween the time. You can also specify a length of a tween. What the video showed you is basically how Roblox made TweenService but why re-invent the wheel? Roblox already have added tons of great features and I think the creator of the video tried to over-simplify things.

How would I tween for time, when the video is about animating parts?
I’m not a scripter or have any knowledge on scripting.

C O N F U S E D

2 Likes

Is this the whole script? Seems like it should make the sun incrementally move, it doesn’t? Did you add some stuff you didn’t post?

So basically all that tween service does it it gives you a gradually incrementing number. You could assign that value to any property that uses the accepted data types. That video is just a general video on tweening. I’m not the best with tweening so you might have to ask someoene else how to pass these values in but I’m pretty sure you can do it.

1 Like

Try one of these examples:

1 Like

Maybe look at this post How to use Tweening for day/night cycle.

1 Like

Hi, I’m not sure about it but try this:

local lighting = game:GetService("Lighting")
local Time = 0.1
local TimeChanged = 0.1

while true do
   wait (Time)
   lighting.ClockTime = lighting.Clocktime + TimeChanged -- or using TimeChanged.Value (I'm not sure)
end 

Tell me if this works, I’ll try my best to help you if it still doesn’t.

1 Like

A script I use When ever I try to make a Day/Night Cycle is.

local dayLength = 12

local cycleTime = dayLength60
local minutesInADay = 24
60

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

If you want day time to last for 5 minutes change the "local dayLength = " to 5. (If your using this Script) Also this will also have the same affect on Night time! Which means it will change too day and night time every 5 minutes.

Hope this helps!

1 Like

If you want to try a tween option, here is a script that I made to test tweening with time. It makes the shadows move very smoothly. I do no know if there is a performance impact to using this:

local daylength = 2 --minutes
local TweenService = game:GetService("TweenService")

local goal1 = {ClockTime = 18}
local goal2 = {ClockTime = 24}
local goal3 = {ClockTime = 6}

local tweenInfo = TweenInfo.new(daylength*60/2, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut)
local tweenInfo2 = TweenInfo.new(daylength*60/2/2, Enum.EasingStyle.Cubic, Enum.EasingDirection.In)
local tweenInfo3 = TweenInfo.new(daylength*60/2/2, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out)

local tween1 = TweenService:Create(game.Lighting, tweenInfo, goal1)
local tween2 = TweenService:Create(game.Lighting, tweenInfo2, goal2)
local tween3 = TweenService:Create(game.Lighting, tweenInfo3, goal3)

tween1:Play()

tween1.Completed:Connect(function()
	game.Lighting.ClockTime = 18
	tween2:Play()
end)

tween2.Completed:Connect(function()
	game.Lighting.ClockTime = 0
	tween3:Play()
end)

tween3.Completed:Connect(function()
	game.Lighting.ClockTime = 6
	tween1:Play()
end)

I used this method of dividing the day up into sections in case I wanted to have a longer day than night or longer night than day. I only used this on a test baseplate, but the shadow movement is nice. Its a bare bone script that plays 3 tweens, each tween triggering the next in line. I think adding a start time constant and clock initialization, and maybe separating out the day length constant into a day length and night length might make it simpler to modify later.

I chose the cubic easing style vs linear in attempt to draw out the sunrise and sunset as the lighting is most dramatic and interesting at these times.

1 Like

In reality your script works fine.
Go into Lighting and manually change the Lighting | Roblox Creator Documentation to a time that’s around 6.0 (6am). This should allow your script to start then, with some small allowance for load times.

1 Like

Hello!
You can follow this tutorial which is really good.

If you are new to scripting, you shouldn’t start with this script. Start with those: game.Workspace.Part.Anchored = true

Yeah, thats the whole script. I didn’t know what to do, so I set Time = 5 and TimeChanged = 5

i made this before and learned from a tutorial a while back, I forget where it was by someone made something similar to this before.

local Lighting = game:GetService("Lighting")
local Minutes = 7 * 60 
local timeroll = true

while timeroll == true do
		Lighting:SetMinutesAfterMidnight(Minutes)
		Minutes = Minutes + 1
		
		wait(2)
end

if i do remember where i learned this from I’ll edit this and link the guy but if you want it to go faster then decrease the wait time though it’ll be moving the sun fast, the 7*60 means it’s 7:00 so if you want it later in the day then change that number.

2 Likes

Try the tween script that I posted.