Sound isnt playing if reached a specific TimeOfDay check for some reason

Hello :wave:
so i made ambient parts for my game but i found it stupid that bird sounds play during night and i dont wanna abrubtly end the audio so i scripted this for a fade in and out and made markers when the sound should play and a night ambient(ignore the fact that there are two because i need two).

local collect = game:GetService("CollectionService")
local Ambient = collect:GetTagged("Ambient")
local light = game:GetService("Lighting")
local TS = game:GetService("TweenService")

for _,part in pairs(Ambient) do
	
	local daysound = part:WaitForChild("ForestAmbient")
	local Nightsound1 = part:WaitForChild("Night Forest Ambient")
	local Nightsound2 = part:WaitForChild("ambient Night")
	
	local DayTweenIn = TS:Create(daysound,TweenInfo.new(5,Enum.EasingStyle.Linear,Enum.EasingDirection.Out),{Volume = 10})
	local DayTweenOut = TS:Create(daysound,TweenInfo.new(5,Enum.EasingStyle.Linear,Enum.EasingDirection.Out),{Volume = 0})
	
	local Night1TweenIn = TS:Create(Nightsound1,TweenInfo.new(5,Enum.EasingStyle.Linear,Enum.EasingDirection.Out),{Volume = 10})
	local Night1TweenOut = TS:Create(Nightsound1,TweenInfo.new(5,Enum.EasingStyle.Linear,Enum.EasingDirection.Out),{Volume = 0})
	
	local Night2TweenIn = TS:Create(Nightsound2,TweenInfo.new(5,Enum.EasingStyle.Linear,Enum.EasingDirection.Out),{Volume = 10})
	local Night2TweenOut = TS:Create(Nightsound2,TweenInfo.new(5,Enum.EasingStyle.Linear,Enum.EasingDirection.Out),{Volume = 0})
	
	
	daysound.Volume = 0
	Nightsound1.Volume = 0
	Nightsound2.Volume = 0
	
	local isDay = true
	
	while task.wait(2) do
		if light.TimeOfDay == "06.00:00" and isDay ~= true then
			isDay = true
			
			daysound:Play()
			DayTweenIn:Play()
			
			Night1TweenOut:Play()
			Night2TweenOut:Play()
			
			task.delay(5, function()
				Nightsound1:Stop()
				Nightsound2:Stop()
			end)
		end

		if light.TimeOfDay == "18.00:00" and isDay ~= false then
			isDay = false
			
			DayTweenOut:Play()
			task.delay(5, function()
				daysound:Stop()
			end)
			
			Nightsound1:Play()
			Night1TweenIn:Play()
			
			Nightsound2:Play()
			Night2TweenIn:Play()
		end
	end
end

The problem well no sound is Playing even it say it should, for some reason it dosent the sounds are directly parented to the parts like this:
Screenshot 2025-05-08 215354
Idk waht the isue is i thought it was the time so i did a debounce(isDay) but still wont work

i hope some one can help me and thank you :slightly_smiling_face:

The format for the TimeOfDay property is going to be hh:mm:ss. In your code, it is hh.mm:ss. The fix here should be to change "06.00:00" to "06:00:00" and do the same thing for the "18.00:00".

about that i actually tried this but thats also not the reason but i changed it to what you just said

You should use the numeric ClockTime instead of exact string matches,
string checks like "06:00:00" almost never hit in a 2s loop

local Clock = Lighting.ClockTime
if Clock >= 6 and Clock < 18 then
-- day
else
-- night
end

or listen to changes directly

Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function() ... end)
1 Like

i change it to this but it only affects now just one part and not all :pensive:

local CollectionService = game:GetService("CollectionService")
local Lighting = game:GetService("Lighting")
local TweenService = game:GetService("TweenService")

local Ambient = CollectionService:GetTagged("Ambient")
local isDay = nil

for _, part in pairs(Ambient) do
	local day = part:WaitForChild("ForestAmbient")
	local night1 = part:WaitForChild("Night Forest Ambient")
	local night2 = part:WaitForChild("ambient Night")

	day.Volume = 0 
	night1.Volume = 0 
	night2.Volume = 0 
	
	
	day.Looped = true
	night1.Looped = true
	night2.Looped = true

	local dayIn = TweenService:Create(day, TweenInfo.new(5), {Volume = 10})
	local dayOut = TweenService:Create(day, TweenInfo.new(5), {Volume = 0})
	local night1In = TweenService:Create(night1, TweenInfo.new(5), {Volume = 10})
	local night1Out = TweenService:Create(night1, TweenInfo.new(5), {Volume = 0})
	local night2In = TweenService:Create(night2, TweenInfo.new(5), {Volume = 10})
	local night2Out = TweenService:Create(night2, TweenInfo.new(5), {Volume = 0})

	Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
		local hour = math.floor(Lighting.ClockTime)

		if hour == 6 and isDay ~= true then
			isDay = true
			if not day.IsPlaying then day:Play() end
			dayIn:Play()
			night1Out:Play()
			night2Out:Play()
			task.delay(5, function()
				night1:Stop()
				night2:Stop()
			end)

		elseif hour == 18 and isDay ~= false then
			isDay = false
			dayOut:Play()
			task.delay(5, function()
				day:Stop()
			end)
			if not night1.IsPlaying then night1:Play() end
			if not night2.IsPlaying then night2:Play() end
			night1In:Play()
			night2In:Play()
		end
	end)
end

You still aren’t using < or > like @tiitxttxitxttttitixi said.
Clock will very rarely equal exactly 6:00:00, so if you use < or > (depending on how you need it set) then if its. 5:59:59 one step and 6:00:01 then the script will realize the number is over 6.

1 Like

Are you 100% sure the sounds have been approved by roblox? Also please make sure to verify your sounds’ properties as they might just be the ones messing you up.

Also, if you parent your sounds to a part, it will play from that part, if you want it to play through the entire map, parent them to Workspace.

no the sound work now but the new problem is it affects only one part and the sound i got from toolbox so there are ok

What do you mean it only affects one part? If it plays only from one part then you need to parent your sounds to workspace as i said.

i have 3 parts having these sounds inside of them (in each of them) these three sound that should play but only one part gets triggerd to play the sound for some reason:

this exact part just there are 3:
Screenshot 2025-05-08 215354

Shouldn’t you place the Ambient loop inside the Lighting:GetPropertyChangedSignal("ClockTime") connection?

I have a feeling it only affects one part because you’re running this connection thrice so it’s overwriting something, similar to how running 2 connections on a TextButton will result in only one of them running…

Sorry in advance for the poor formatting…

local CollectionService = game:GetService("CollectionService")
local Lighting = game:GetService("Lighting")
local TweenService = game:GetService("TweenService")

local Ambient = CollectionService:GetTagged("Ambient")
local isDay = nil

Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
for _, part in pairs(Ambient) do
	local day = part:WaitForChild("ForestAmbient")
	local night1 = part:WaitForChild("Night Forest Ambient")
	local night2 = part:WaitForChild("ambient Night")

	day.Volume = 0 
	night1.Volume = 0 
	night2.Volume = 0 
		
	day.Looped = true
	night1.Looped = true
	night2.Looped = true

	local dayIn = TweenService:Create(day, TweenInfo.new(5), {Volume = 10})
	local dayOut = TweenService:Create(day, TweenInfo.new(5), {Volume = 0})
	local night1In = TweenService:Create(night1, TweenInfo.new(5), {Volume = 10})
	local night1Out = TweenService:Create(night1, TweenInfo.new(5), {Volume = 0})
	local night2In = TweenService:Create(night2, TweenInfo.new(5), {Volume = 10})
	local night2Out = TweenService:Create(night2, TweenInfo.new(5), {Volume = 0})

	local hour = math.floor(Lighting.ClockTime)

	if hour == 6 and isDay ~= true then
		isDay = true
		if not day.IsPlaying then day:Play() end
		dayIn:Play()
		night1Out:Play()
		night2Out:Play()
		task.delay(5, function()
			night1:Stop()
			night2:Stop()
		end)
	elseif hour == 18 and isDay ~= false then
		isDay = false
		dayOut:Play()
		task.delay(5, function()
			day:Stop()
		end)
		if not night1.IsPlaying then night1:Play() end
		if not night2.IsPlaying then night2:Play() end
		night1In:Play()
		night2In:Play()
	end
end
end)

After some while i restrucktured the entire code and now it works perfectlie fine thanks you all for your help :saluting_face:

local CollectionService = game:GetService("CollectionService")
local Lighting = game:GetService("Lighting")
local TweenService = game:GetService("TweenService")

local AmbientParts = CollectionService:GetTagged("Ambient")
local isDay = nil

-- Preload and setup all sounds
for _, part in pairs(AmbientParts) do
	local day = part:WaitForChild("ForestAmbient")
	local night2 = part:WaitForChild("ambient Night")

	-- Start all sounds at Volume 0
	for _, sound in pairs({day, night2}) do
		sound.Volume = 0
		sound.Looped = true
		sound:Play()
	end
end

-- Transition helper
local function fadeVolume(sound, targetVolume)
	if sound then
		local tween = TweenService:Create(sound, TweenInfo.new(5), {Volume = targetVolume})
		tween:Play()
	end
end

-- Time-based transition
local function updateAmbience()
	local hour = math.floor(Lighting.ClockTime)

	if hour == 6 and isDay ~= true then
		isDay = true
		for _, part in pairs(AmbientParts) do
			local day = part:FindFirstChild("ForestAmbient")
			local night2 = part:FindFirstChild("ambient Night")

			fadeVolume(day, 10)
			fadeVolume(night2, 0)
		end

	elseif hour == 18 and isDay ~= false then
		isDay = false
		for _, part in pairs(AmbientParts) do
			local day = part:FindFirstChild("ForestAmbient")
			local night2 = part:FindFirstChild("ambient Night")

			fadeVolume(day, 0)
			fadeVolume(night2, 10)
		end
	end
end

-- Initial check in case it's already night/day
updateAmbience()

-- Listen to ClockTime changes once
Lighting:GetPropertyChangedSignal("ClockTime"):Connect(updateAmbience)

Good to know, I thought that was the problem :smiley:

1 Like