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

How could I make this day/night cycle script more efficient?

As you've noticed (by how many questions I've asked) I'm a beginner on my 4th(?) day of learning how to script. For my current project, I have scripted this using some help with functions from the wikia. Although I'm very proud of what a useful script I've made, it however bugs me about the length and structure of the code. Could I get some ideas about the script and how to make it better and more efficient? For example - Should I use if hour % 24 == 0 instead of if hour > 23? Thanks in advance!

hour = 0
day = 0 
minutes = 0

while true do
	wait(1)
	minutes = minutes + 1
	if minutes > 60 then
		minutes = 0
		hour = hour + 1
	end
	game.Lighting:SetMinutesAfterMidnight(hour*60)
	if hour > 23 then
		hour = 0
		day = day + 1
	end	
	print("It is day " .. tostring(day) .. tostring(" and ") .. tostring(minutes) .. tostring(" minutes past ") .. tostring(hour))
end


1 answer

Try this:

Minutes

while wait(60) do
	game.Lighting:SetMinutesAfterMidnight(game.Lighting:GetMinutesAfterMidnight() + 1)
end

Seconds

while wait(1) do
	game.Lighting:SetMinutesAfterMidnight(game.Lighting:GetMinutesAfterMidnight() + 0.0167)
end

2 comments

0

Hi! I want to keep the seconds variable and, forgive me if I'm wrong, that replaces it?

0

To get the time, use game.Lighting.TimeOfDay

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