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
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
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
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.