How do I make sounds play at a specific time? I am trying to make this phone play these sounds when it turns midnight. I'm not getting any errors, but the sounds aren't playing. Like, at all. And I am also trying to get them to play at different days of the week, but I don't know if it's the day tracker on here that's messing everything up or not.
local Night = 1 while true do wait(0.5) if game.Lighting.ClockTime == 0 then if Night == 1 then script.Parent.Night1:play() print(Night) Night + 1 end if Night == 2 then script.Parent.Night2:play() print(Night) Night + 1 end if Night == 3 then script.Parent.Night3:play() print(Night) Night + 1 end if Night == 4 then script.Parent.Night4:play() print(Night) Night + 1 end if Night == 5 then script.Parent.Night5:play() print(Night) Night + 1 end if Night == 6 then print(Night) Night + 1 end if Night == 7 then print(Night) Night - 6 end else wait() end end
And here is the code for the script that changes the time.
local dayLength = 24 --How many minutes a full day(24 hours) will last local cycleTime = dayLength*60 local minutesInADay = 24*60 local lighting = game:GetService("Lighting") local startTime = tick() - (lighting:GetMinutesAfterMidnight()/ minutesInADay)*cycleTime local endTime = startTime + cycleTime local timeRatio = minutesInADay / cycleTime if dayLength == 0 then dayLength = 1 end repeat local currentTime = tick() if currentTime > endTime then startTime = endTime endTime = startTime + cycleTime end lighting:SetMinutesAfterMidnight((currentTime - startTime)*timeRatio) wait(1/15) until false
How are you changing the time in your game?
Swap out the while loop with a :GetPropertyChangedSignal("ClockTime")
.
What I see here is that all 5 sounds play all at once if that's what you want otherwise, you can add something like this
script.Parent.Night1.Ended:Wait()
Always avoid using wait(n)
, the best solution is using :Wait()
with events.
When changing the time or doing something like this, always do it on the Server via Script.
local Lighting = game:GetService("Lighting") local Night = 1 Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function() if Lighting.ClockTime == 0 then if Night == 1 then script.Parent.Night1:play() script.Parent.Night1.Ended:Wait() print(Night) Night + 1 end if Night == 2 then script.Parent.Night2:play() script.Parent.Night2.Ended:Wait() print(Night) Night + 1 end if Night == 3 then script.Parent.Night3:play() script.Parent.Night3.Ended:Wait() print(Night) Night + 1 end if Night == 4 then script.Parent.Night4:play() script.Parent.Night4.Ended:Wait() print(Night) Night + 1 end if Night == 5 then script.Parent.Night5:play() script.Parent.Night5.Ended:Wait() print(Night) Night + 1 end if Night == 6 then print(Night) Night + 1 end if Night == 7 then print(Night) Night - 6 end end end