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

Time of day, different song every 12 hour in game issue?

So im having another scripting issue... I have two songs that i would like to be played but at different times, one at 6am and the other at 6pm and this method works fine until it reaches 6pm again. Not sure what im doing wrong on this..

local Song = game.Workspace.PalletTown1.Dakota
local Song1 = game.Workspace.PalletTown1.Dakota1

while true do
	wait(.1)
 
	if game.Lighting.TimeOfDay == "06:00:00"
		then 
		Song1:Stop ()
		wait(.1)
		Song:Play()
		Song.Looped = true
	print("Day Song is playing")
	end
	if game.Lighting.TimeOfDay == "18:00:00"
		then 
		Song:Stop ()
		wait(0.1)
		Song1: Play()
		Song1.Looped = true
	print("Night Song is playing")

	end
end

1 answer

✓ Accepted answer
Song = game.Workspace.PalletTown1.Dakota
Song1 = game.Workspace.PalletTown1.Dakota1
minutesAfterMidnight = 0
 
while true do
	-- remove these lines if you don't want a new day/night cycle.
	minutesAfterMidnight = minutesAfterMidnight + 1 -- Speed of the day/night cycle. Remove 
	game.Lighting:SetMinutesAfterMidnight(minutesAfterMidnight)
	wait(0.1)  
	-- This basically takes whatever time it is in the game, and constantly adds one to the time every 0.1 seconds, as the while true do loop constantly keeps it running. And starts over once finished.
	
	
	if game.Lighting:GetMinutesAfterMidnight() == 6 * 60 then -- checks for 6AM
		Song1:Stop()
		wait(.1)
		Song:Play()
		Song.Looped = true
		print("Day song playing.")
	end
	if game.Lighting:GetMinutesAfterMidnight() == 18 * 60 then		-- checks for 6PM
		Song:Stop()
		wait(.1)
		Song1:Play()
		Song.Looped = true
		print("Night song playing.")
	end
end

The while true do is an event which constantly runs any code between it, over and over until there is a break. The first if statement is constantly called to check when it is 6 AM, once that is true it runs any code after it until it ends. The second if statement is constantly called to check for when it is 6 PM, once that is true, it runs the code after it. If you wish not to have a day/night cycle, remove the first 3 lines of code underneath the while true do

Hope this helps!

7 comments

0

wouldnt this create a new Day/Night Cycle? If so i already have on in a separate script. I wouldnt be able to combine them like this bec my game will have separate area in the game.

0

Are you able to just use this script? Or not?

0

You have to be able to explain the script for him to understand it.

0

To prevent it from making a new day/night cycle, just remove the first 3 lines under the 'while true do'.

0

probably not bec when my players go to a different area it will have a different music for that area. Which is why i had my day cycle code in another script, if i did it this way it would make 2 day cycles and cause an error. Basically what im focusing on is if your in this town it will start off in the day song then once night hit i would want the night time song to play, but i wouldnt want thi

0

Just remove the 3 lines of code underneath the 'while true do'.

0

this song for another town or a route bec then it would be too plain. Thats why i had the TimeOfDay function instead of SetMinutesAfterMidnight

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