Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Help with Solving Day/Night Music Script Issue?

Asked by 2 years ago

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!

1 answer

Log in to vote
2
Answered by 2 years ago
Edited 2 years ago

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()
1
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 Astrosupernuat 29 — 2y
0
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. Flipstargamer 90 — 2y
Ad

Answer this question