Day/night sound

I have a good script that makes a night and day sound, but this sound ends in the middle of the night / day and does not repeat.
Script:

local t = ""

local function fadeIn(audio)
	audio:Play()
	for i = 0,.5,.01 do
		audio.Volume = i
		wait(.1)
	end
end

local function fadeOut(audio)
	for i = .5,0,-.01 do
		audio.Volume = i
		wait(.1)
	end
	audio:Stop()
end

repeat
	if game.Lighting:GetMinutesAfterMidnight() < 6*60 or game.Lighting:GetMinutesAfterMidnight() > 18*60 then
		lastT = t
		t = "night"
	else
		lastT = t
		t = "day"
	end
	if lastT ~= t then
		if t == "night" then
			fadeOut(script.day)
			fadeIn(script.night)
		elseif t == "day" then
			fadeOut(script.night)
			fadeIn(script.day)
		end
	end
until not wait(1)

structure:image

7 Likes
script.Night.Volume = 0
script.Day.Volume = 0
script.Day.Looped = true
script.Night.Looped = true
script.Night:Play()
script.Day:Play()
local function TweenVolume(Sound, Goal, Time)
	local Tween = game:GetService("TweenService"):Create(Sound, TweenInfo.new(Time), {Volume = Goal})
	Tween:Play()
	return Tween
end
local LastState = nil
game.Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
	local IsDay = nil
	if (game.Lighting.ClockTime >= 0 and game.Lighting.ClockTime <= 6) or game.Lighting.ClockTime >= 18 then
		IsDay = false
	else
		IsDay = true
	end
	if IsDay ~= LastState then
		LastState = IsDay
		if IsDay == true then
			TweenVolume(script.Night, 0, 3)
			TweenVolume(script.Day, 1, 4)
		else
			TweenVolume(script.Night, 1, 4)
			TweenVolume(script.Day, 0, 3)
		end
	end
end)

do not working. Idk what i must do…

1 Like

Works fine in a regular script under ServerScriptService

the sounds should be named “Day” & “Night” as children of the script

1 Like

The sound of the night does not turn on on the second day

May I suggest using a LocalScript instead?
Make sure your ambience loops!

I also made my own modification of your script so it uses while true do. It’s much better in my opinion.

1 Like

The way you are getting day/ is rather weird, maybe thats why?
You should make your day/night cycle script send a signal to other scripts. Mine look like this

				while 1 do
					LoopUntil(Cycle[Index]) --function where you increment the timeofday/clocktime

					Callback() -- here
					
					Index += 1
					if Index > #Cycle then
						Index = 1
					end
				end

The cycle should look like this {[1] = someclocktimethatstheearliest, [2] = thesecondclocktimethatsafterthefirst....etc}

It does… but here is an updated script and video example

local DayData = {
	["Looped"] = false,
	["Volume"] = 1
}
local NightData = {
	["Looped"] = true,
	["Volume"] = 1
}




if DayData.Looped then
	script.Day.Volume = 0
	script.Day.Looped = true
	script.Day:Play()
end
if NightData.Looped then
	script.Night.Volume = 0
	script.Night.Looped = true
	script.Night:Play()
end
local function TweenVolume(Sound, Goal, Time)
	local Tween = game:GetService("TweenService"):Create(Sound, TweenInfo.new(Time), {Volume = Goal})
	Tween:Play()
	return Tween
end
local LastState = nil
game.Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
	local IsDay = nil
	if (game.Lighting.ClockTime >= 0 and game.Lighting.ClockTime <= 6) or game.Lighting.ClockTime >= 18 then
		IsDay = false
	else
		IsDay = true
	end
	if IsDay ~= LastState then
		LastState = IsDay
		if IsDay == true then
			if not DayData.Looped then script.Day:Play() end
			TweenVolume(script.Night, 0, 3)
			TweenVolume(script.Day, 1, 4)
		else
			if not NightData.Looped then script.Night:Play() end
			TweenVolume(script.Night, 1, 4)
			TweenVolume(script.Day, 0, 3)
		end
	end
end)

https://streamable.com/ldm8yc

4 Likes