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

How to make sounds play at specific times of the day?

I have script for a 60 minute day cycle, which means its day for 30 minutes and night for 30 minutes. I am new to scripting and I attempted to create a script that will loop music during the day, and a different music at night.

game.Players.PlayerAdded:connect(function(player)
	player.CharacterAdded:connect(function()
		repeat wait() until player.Character
        if game.Lighting.ClockTime > 5 and game.Lighting.ClockTime < 17 then
            script.Day:Play()
            script.Night:Stop()
        if game.Lighting.ClockTime > 17 and game.Lighting.ClockTime < 5 then
            script.Night:Play()
            script.Day:Stop()
        end
		end
	end)
end)

The layout is as follows:

LocalScript Day Sound Night Sound

This script isn't working and plays nothing at all. Advice? Please be descriptive!

1 answer

✓ Accepted answer

You need to use a while loop to constantly check if it is day/night. If you don't, the game will just check once and never check afterwards.

game.Players.PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function()
        repeat wait() until player.Character
   	   while wait() do
              if game.Lighting.ClockTime > 5 and game.Lighting.ClockTime < 17 then
                  script.Day:Play()
                  script.Night:Stop()
              if game.Lighting.ClockTime > 17 and game.Lighting.ClockTime < 5 then
                  script.Night:Play()
                  script.Day:Stop()
              end
	   end
        end
    end)
end)

4 comments

0

Thanks, this helped a lot! But when I tried this script, the audio was continuously Playing over and over, so I changed Play() to Resume(), and that fixed that problem, but now the Day only plays and Night never plays, and Day never stops. Why?

0

use the changed event. game.Lighting.Changed:connect(function()

0

(REST OF SCRIPT)

0

end)

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