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

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

Asked by 6 years ago
Edited 6 years ago

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

Log in to vote
0
Answered by 6 years ago

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)

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? ShinyGriffin 129 — 6y
0
use the changed event. game.Lighting.Changed:connect(function() wookey12 174 — 6y
0
(REST OF SCRIPT) wookey12 174 — 6y
0
end) wookey12 174 — 6y
Ad

Answer this question