So im having another scripting issue... I have two songs that i would like to be played but at different times, one at 6am and the other at 6pm and this method works fine until it reaches 6pm again. Not sure what im doing wrong on this..
local Song = game.Workspace.PalletTown1.Dakota local Song1 = game.Workspace.PalletTown1.Dakota1 while true do wait(.1) if game.Lighting.TimeOfDay == "06:00:00" then Song1:Stop () wait(.1) Song:Play() Song.Looped = true print("Day Song is playing") end if game.Lighting.TimeOfDay == "18:00:00" then Song:Stop () wait(0.1) Song1: Play() Song1.Looped = true print("Night Song is playing") end end
Song = game.Workspace.PalletTown1.Dakota Song1 = game.Workspace.PalletTown1.Dakota1 minutesAfterMidnight = 0 while true do -- remove these lines if you don't want a new day/night cycle. minutesAfterMidnight = minutesAfterMidnight + 1 -- Speed of the day/night cycle. Remove game.Lighting:SetMinutesAfterMidnight(minutesAfterMidnight) wait(0.1) -- This basically takes whatever time it is in the game, and constantly adds one to the time every 0.1 seconds, as the while true do loop constantly keeps it running. And starts over once finished. if game.Lighting:GetMinutesAfterMidnight() == 6 * 60 then -- checks for 6AM Song1:Stop() wait(.1) Song:Play() Song.Looped = true print("Day song playing.") end if game.Lighting:GetMinutesAfterMidnight() == 18 * 60 then -- checks for 6PM Song:Stop() wait(.1) Song1:Play() Song.Looped = true print("Night song playing.") end end
The while true do
is an event which constantly runs any code between it, over and over until there is a break
. The first if
statement is constantly called to check when it is 6 AM, once that is true it runs any code after it until it ends. The second if
statement is constantly called to check for when it is 6 PM, once that is true, it runs the code after it. If you wish not to have a day/night cycle, remove the first 3 lines of code underneath the while true do
Hope this helps!