Music changes depending on the game's time?

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`
3 Likes

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

The code you had above will play at the 18(clock time) and 17(clock time) only

Here’s what I put

  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)
1 Like

Thanks it worked!
here’s it working in-game

(Note: The time changing was set to 2 minutes instead of 60)