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

How do I fix while true do script from repeating the first section?

Asked by 4 years ago

I'm trying to make a script where it plays one song at day and another at night. But when it becomes night, (I believe) both songs keep interfering with eachother and repeats the night one every 5 seconds from the sunset till the moon is in the middle of the sky, how can I fix this?

It does not do the same with the day song from sunrise to the middle, the song just loops like normal.

(I'm also trying to expand the script so the music only play when you're inside a part, (a region part if I'd say) if it helps with slimming it down or avoiding some kind of confusion for when I get to that part.)

local DaySong = game.workspace.Music.Main
local NightSong = game.workspace.Music.News
if not game.workspace.Music.Main.IsPlaying then
    game.workspace.Music.Main:Play()
end

while true do
    wait(5)
    if game.Lighting:GetMinutesAfterMidnight() > 6 * 60 then 
        if NightSong.IsPlaying and not DaySong.IsPlaying then
            NightSong:stop()
            wait(.5)
            DaySong:play()
            print("Working660")
        end
    end
    if game.Lighting:GetMinutesAfterMidnight() > 16 * 60 then
        if DaySong.IsPlaying and not NightSong.IsPlaying then
            DaySong:stop()
            wait(.5)
            NightSong:play()
            print("Working1660")
        end
    end
end

1 answer

Log in to vote
1
Answered by
megukoo 877 Moderation Voter
4 years ago
Edited 4 years ago

In your first if statement, check if it is above 6*60 and below 16*60. This will set up logic that says, "I'll only play it during this timeframe."

local minutesAfter = game.Lighting:GetMinutesAfterMidnight()
if minutesAfter > 6 * 60 and minutesAfter < 16 * 60 then 
Ad

Answer this question