Hi there! I'm currently working on a farming game as a first project and as of now I'm trying to solve an issue with this script that essentially plays two different music tracks at different times of the day. One track for daytime and one for night time. However, I cannot get this to work properly somehow because for example the night track still plays even after the sun starts rising, with the daytime music not playing at all. I would like it if someone helps me address this issue while also enlightening me with some helpful info on how time in roblox works if you can because that would be extremely helpful!
Here's the code:
local nightMusic = game.SoundService.GuitarNight
local dayMusic = game.SoundService.GuitarsDay
while true do
wait()
if game.Lighting.ClockTime <= 6 and game.Lighting.ClockTime < 18 then
print("Playing Day Music")
nightMusic:Stop()
dayMusic:Play()
elseif game.Lighting.ClockTime <= 6 or game.Lighting.ClockTime >= 18 then
print("Playing Night Music")
dayMusic:Stop()
nightMusic:Play()
end
end
Thanks!

2 comments
Thanks a lot! Quick thing to note here is that when executing this version of the code, the music stops and keeps looping back to the beginning after 1 second, is there a way to counteract this? using a wait() doesnt seem to work
I have a reason for this. When the Play is called on the sound it starts from the beginning of the script. I'll update the answer it with a fix.