Time of day sound play

I am trying to get a couple sounds to play on my day/night cycle script. I added a while true do statement but that isnt working.

local lighting = game:GetService("Lighting")

local minutes = 7 * 60

while wait() do
	lighting:SetMinutesAfterMidnight(minutes)
	minutes = minutes + .1
end

local morning = game.Workspace.timesounds.morning
local night = game.Workspace.timesounds.night

while true do

lighting.ClockTime = 18
night:Play()

lighting.Clocktime = 7
	morning:Play()
end

what do you guys think?

1 Like

So first off, scripts run on a single thread by default. If you have an endless loop, the code afterwards won’t run. But give me a moment as I write something.

1 Like

Do you expect the sounds to play until the next time change, or just play once and end until the next time change?

Yep, like @MightyDantheman says, your script stops here:
image

1 Like

just one single play and the sounds are short

Well, I suppose this will still work for you then. I tried to quickly throw something together:

local T = game:GetService('TweenService')
function tween(o,t,l,s,d)
	s = s or Enum.EasingStyle.Linear
	d = d or Enum.EasingDirection.InOut
	local i = TweenInfo.new(l,s,d)
	return T:Create(o,i,t)
end

local L = game:GetService('Lighting')
local sounds = workspace:WaitForChild('Time Sounds')
local morning = sounds:WaitForChild('Morning')
local night = sounds:WaitForChild('Night')
local playing

function transition(a,b,l,v)
	l = l or 1
	v = v or 0.5
	tween(a,{Volume=0},l):Play()
	wait(l)
	a:Stop()
	b:Play()
	tween(a,{Volume=v},l):Play()
end

L.Changed:connect(function()
	if playing ~= morning and L.ClockTime >= 6 and L.ClockTime < 18 then
		playing = morning
		transition(night,morning,2,0.5)
	elseif playing ~= night and (L.ClockTime < 6 or L.ClockTime >= 18) then
		playing = night
		transition(morning,night,2,0.5)
	end
end)

repeat
	wait(.01)
	L.ClockTime+=0.01
until false

Though you’ll probably have to adjust the time scale and numbers to fit what you want. But considering you only want to play a sound once, this is a bit inefficient. I might rewrite this just for that. I made this assuming you’d have constant ambient sounds in the background.

Actually, as it turns out, just removing some stuff should be good enough:

local L = game:GetService('Lighting')
local sounds = workspace:WaitForChild('Time Sounds')
local morning = sounds:WaitForChild('Morning')
local night = sounds:WaitForChild('Night')
local playing

L.Changed:connect(function()
	if playing ~= morning and L.ClockTime >= 6 and L.ClockTime < 18 then
		playing = morning
		morning:Play()
	elseif playing ~= night and (L.ClockTime < 6 or L.ClockTime >= 18) then
		playing = night
		night:Play()
	end
end)

repeat
	wait(.01)
	L.ClockTime+=0.01
until false
1 Like

Thanks I am testing this right now.

I could have explained better, but the sounds of morning is a rooster, and night is a howling wolf. So they only needed to be played one time.

Thanks again Ill see if this works. I just put this in a new script.

Ironically, I tested that with a rooster and an owl. For the continuous sounds, I tested with birds and crickets.

As long as the pathing names are correct and the sounds aren’t set to loop, it should work.

1 Like

sweet thanks. I was just finishing up another script test, so I can test both at the same time.

I took what you wrote and put it in a new script under serverscriptservice.

It plays both sounds at the same time. Roster and wolf over the top of each other.

Make sure the pathing is correct and that if you’ve changed anything, make sure it’s consistent. It worked as intended during my own test.

its weird it plays both unless I change the time slightly. However there is a problem with the script checking the time frame and then playing the sound.

Anyone who logs in during the day will hear the rooster. Even if it is 5pm.

A script that plays the sound when the time hits 7 and 18 would be cleaner. I am still trying to work that out.

Make sure the numbers match on both sides. If you’re changing 6 to 7, make sure both 6’s are changed to 7.

Also, it should only play it once when the server starts, not when players join. But if you’d like to fix that, you can replace:

local playing

With:

local playing = morning
1 Like

haha the night sound plays now when the server starts

Well what time do you have set for the server to start at?

1 Like

it starts at 7.

repeat
	wait(1)
	L.ClockTime+=0.01
until false

I changed the wait to 1 and it stopped both sounds from playing at the same time.

And I changed the times on the script to 6.9 and 18.5 and it got rid of the night sound on server start.

It’s weird that they played at the same time to begin with. It worked fine for me without any changes. It could be that I started somewhere in the middle of the day, but you could try changing the starting time to 7.1 or something instead of changing the other things.

Also, as far as the sound playing on server start, you shouldn’t notice this as a player anyway.