Hello, I want to make a script that changes the music on the time of the day. I have tried this before and have failed many times. Here’s the code
`while true do
if game.Lighting.ClockTime == 18 then
workspace.Sound.SoundId = "rbxassetid://1843415082"
end
if game.Lighting.ClockTime == 7 then
workspace.Sound.SoundId = "rbxassetid://1836688300"
end`
You should probably do game:GetService'Lighting':GetPropertyChangedSignal'ClockTime' and then check if the ClockTime falls in a certain range and then play the corresponding music
if Time > 7 and Time <= 18 then
-- play day music
elseif Time > 18 or Time <= 7 then
-- play night music
end
You should also make it check whether it’s already playing the correct music to prevent it from repeatedly resetting the music
while true do
local Time = game.Lighting.ClockTime
game:GetService'Lighting':GetPropertyChangedSignal'ClockTime'
if Time > 7 and Time <= 18 then
-- play day music
workspace.Sound.SoundId = "rbxassetid://1836688300"
elseif Time > 18 or Time <= 7 then
-- play night music
workspace.Sound.SoundId = "rbxassetid://1843415082"
end
end
local DayMusic = "rbxassetid://1836688300"
local NightMusic = "rbxassetid://1843415082"
local Music = workspace.Sound
local Lighting = game:GetService'Lighting'
Lighting:GetPropertyChangedSignal'ClockTime':Connect(function()
local Time = Lighting.ClockTime
if Time > 7 and Time <= 18 and Music.SoundId ~= DayMusic then
Music:Stop()
Music.SoundId = DayMusic
Music:Play()
elseif (Time > 18 or Time <= 7) and Music.SoundId ~= NightMusic then
Music:Stop()
Music.SoundId = NightMusic
Music:Play()
end
end)