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!
There are two issues: Your if statement and the parent of the sounds.
The if statement's problem is both statements say if the value is greater than or equal to 6 basically means both are correct for it, which for this case it shouldn't be like that.
Fix for this should look like:
if game.Lighting.ClockTime >= 6 and game.Lighting.ClockTime < 18 then -- Code here elseif game.Lighting.ClockTime < 6 or game.Lighting.ClockTime >= 18 then -- Code here end
The parent problem is because sound service is only to determine how sound plays and not to play sounds globally, so you should put it in workspace or a folder in workspace.
Also, the sound will start from the start, but this is a simple fix. The fix is just to check if the sound is currently playing if it is then don't play the sound again. Example:
if not SoundVar.Playing then -- SoundVar shouldn't be used SoundVar:Play() end
There are also several code improvements:
First one is that instead of using game.Lighting
use game:GetService("Lighting")
.
Second one is that instead of repeating getting the service make it a variable.
Third one is instead of doing a while loop instead connect a check function to the clock time property changed signal and check it after the connection (to play music when game starts). Example:
local function check() -- Code Here end lighting:GetPropertyChangedSignal("ClockTime"):Connect(check) check()
After fixes and improvements:
local nightMusic = workspace.GuitarNight local dayMusic = workspace.GuitarsDay local lighting = game:GetService("Lighting") local function check() if lighting.ClockTime >= 6 and lighting.ClockTime < 18 then if not dayMusic.Playing then print("Playing Day Music") nightMusic:Stop() dayMusic:Play() end elseif lighting.ClockTime < 6 or lighting.ClockTime >= 18 then if not nightMusic.Playing then print("Playing Night Music") dayMusic:Stop() nightMusic:Play() end end end lighting:GetPropertyChangedSignal("ClockTime"):Connect(check) check()