How to make the audio stop at night?

I’m trying to make the audio stop playing at night, everything else works but i cannot figure out what to do to make the script pause the music at night. I have tried many things and i feel its a really easy fix but it’s still not working

local currentSoundIndex = nil
local sounds = nil

local musicFolder = game.Workspace.Music

sounds = musicFolder.Sounds:GetChildren()

if #sounds == 0 then
print(“No sound files found in the folder.”)
else
while true do
if game.Lighting.ClockTime >= 6 and game.Lighting.ClockTime < 18 then
– Pause current song if there is one
if currentSoundIndex then
local sound = sounds[currentSoundIndex]
if sound and sound:IsA(“Sound”) then
sound:Pause()
end
end
local randomIndex = math.random(1, #sounds)
currentSoundIndex = randomIndex
local sound = sounds[currentSoundIndex]
if sound and sound:IsA(“Sound”) then
sound.Volume = 0
sound:Play()
game:GetService(“TweenService”):Create(sound, TweenInfo.new(2), {Volume = 1}):Play()

			sound.Ended:Wait()
			game:GetService("TweenService"):Create(sound, TweenInfo.new(2), {Volume = 0}):Play()
			local waitTime = math.random(10, 60)
			wait(waitTime)
		end
	else
		-- Handle night time behavior here
		for _, sound in ipairs(sounds) do
			if sound and sound:IsA("Sound") then
				sound:Stop()
			end
		end
	end 
	wait(1) 
end 

end

1 Like

Where’s the sound that’s playing located?

2 Likes

It is under a folder in workspace, it plays the audio it just will not pause it.

2 Likes

idk if this is correct but is there an else to split the day and night functions (format of the code is a little weird in the original post)

if (#sounds == 0) then
	warn("No sound files found in the folder.");
else
	task.spawn(function() -- new thread so it doesnt inf yield if you have stuff after this one thingy
		local t:number
		while true do
			t = game:GetService("Lighting").ClockTime;
			if (t >= 6 and t < 18) then
				-- Day Time Stuff
			else
				-- Night Time
			end
			task.wait(); -- buffer so you dont crash lol
		end
	end)
end
2 Likes

Yes, i have also tried to to use clocktime is night and it still hasn’t worked.

1 Like

nvm thank you for your help, but I figured it out! I put the part that pauses the audio in a separate script and it worked! :slight_smile:

1 Like
local currentSoundIndex = nil
local sounds = nil

local musicFolder = game.Workspace.Music
sounds = musicFolder.Sounds:GetChildren()

if #sounds == 0 then
    print("No sound files found in the folder.")
else
    while true do
        if game.Lighting.ClockTime >= 6 and game.Lighting.ClockTime < 18 then
            if currentSoundIndex then
                local sound = sounds[currentSoundIndex]
                if sound and sound:IsA("Sound") then
                    sound:Pause()
                end
            end
            local randomIndex = math.random(1, #sounds)
            currentSoundIndex = randomIndex
            local sound = sounds[currentSoundIndex]
            if sound and sound:IsA("Sound") then
                sound.Volume = 0
                sound:Play()
                game:GetService("TweenService"):Create(sound, TweenInfo.new(2), {Volume = 1}):Play()
                sound.Ended:Wait()
                game:GetService("TweenService"):Create(sound, TweenInfo.new(2), {Volume = 0}):Play()
                local waitTime = math.random(10, 60)
                wait(waitTime)
            end
        else
            break
        end
        wait(1)
    end

    for _, sound in ipairs(sounds) do
        if sound and sound:IsA("Sound") then
            sound:Stop()
        end
    end
end

1 Like

Thank you, weirdly it still didn’t work, but no worry’s because I already fixed it!

1 Like

Hmm, tried to follow what you had. But there was a few things about it I wasn’t sure of. Like that break and how it’s getting a reference on the sounds. Glad you figured it out. I didn’t test this one.

1 Like

Np, it is weird because most things that should work wouldn’t. But ty!

1 Like