Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Day/Night

I feel really stupid D: Not the best with making scripts, but how do I create a basic day/night script?

1 comment

0

Theres also day/night scripts in the catalog that work just fine you just put in how fast you want the cycle.

4 answers

✓ Accepted answer

In many of my games, I use code similar to this to change the time of day at a rate 60 times as fast as real time:

while true do
	    Game:GetService('Lighting'):SetMinutesAfterMidnight(Game:GetService('Lighting'):GetMinutesAfterMidnight() + wait())
end

The reason this is 60 times as fast as real time is because the wait function returns the time it waited in seconds, while GetMinutesAfterMidnight expects minutes. If you want the in-game time to correspond to real time, multiply the result of the call to wait by 60. You can multiply or divide it by other values to make it faster or slower.

This solution is optimal and better than previously described ones because it adds the result of the call to wait to the time. This means it takes into consideration the fluctuations in the time that is actually waited for. It therefore provides a more stable time cycle, while other the other solutions described add a certain amount of time to the game time every time wait returns, which can happen at varying periods of time.

l = game:service("Lighting")
while true do
	l:SetMinutesAfterMidnight(l:GetMinutesAfterMidnight()+.5)
	wait(.05)
end

The easiest way to change the time mathematically is to use the :SetMinutesAfterMidnight() method of the lighting. The concept goes like this:

  • Get the minutes after midnight
  • Add a small amount to that number
  • Set the minutes after midnight to the new number
local lighting = Game.Lighting

while wait() do
	local newTime= lighting:GetMinutesAfterMidnight();
	lighting:SetMinutesAfterMidnight( newTime + .5 )
end

Alright a basic way to create a day and night script is

while wait(1/2) do
	Game.Lighting:SetMinutesAfterMidnight(Game.Lighting:GetMinutesAfterMidnight()+1/5)
end

The way this works is that we have a while loop that runs every half a second. Then we use SetMinutesAfterMidnight based off GetMinutesAfterMidnight and after that we add a 1/5 of a minute.

(So to make this faster or slower change the looping time or the addition of minutes time)

curse you MrNicNac

This discussion is preserved as it was. New answers, comments, and votes are closed.