Day and Night Music Script Not Working

Hey!
I want to make a script where if theres a specific time of day, a music plays (like a background music) and if it turns night, a different kind of music plays. I’m having trouble making that. One of the troubles are that NightTime music keeps on spamming even though I added a debounce. Another problem is it plays even though it isnt at the designated time

Here is my line of code.

--SOUNDS
local Kano = script.Kano
local Snowfall = script.Snowfall
local TwentyFour = script.TwentyFour
local DayMusic = script.DayMusic

--VARIABLES
local Lighting = game.Lighting
local Night = false
local Day = true

--MAIN SCRIPT
Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
	if Lighting.ClockTime < 18 or Lighting.ClockTime >= 6.18 and Night == false then
	Day = false
	Night = true
	DayMusic:Stop()
	wait(5)
	local NightMusic = math.random(1,3)
	if NightMusic == 1 then
		Kano:Play()
	end
	if NightMusic == 2 then
        Snowfall:Play()
	end
	if NightMusic == 3 then
       TwentyFour:Play()
	end
	print("Night Time!")
	end
end)
Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
	if Lighting.ClockTime >= 18 or Lighting.ClockTime < 6.18 and Day == false then
		Day = true
		Night = false
		Kano:Stop()
		Snowfall:Stop()
		TwentyFour:Stop()
		wait(5)
	DayMusic:Play()
	print("Day Time!")
	end
end)

I’ve tried to look for solutions in the DevForums but none of them are helping.

(Kano, Snowfall, and TwentyFour are all NightTime musics)

If anyone can help me solve this that would be greatly appreciated. Thanks!

Look at outlets they can help you if sometimes wrong or good m. So check that if anything is wrong

The output? I’ve looked at it. There aren’t any errors, it’s just that something about my script doesn’t let it run the way I want it to.

1 Like

Well check if u wrote something wrong or left any spaces that can make ur script not work too.

Okay, I’ve looked at it,
I don’t see any errors in my script. I think it’s just the way I’ve programmed my code.

Yeah that can be the problem or forgot something to add

Try this

--SOUNDS
local Kano = script.Kano
local Snowfall = script.Snowfall
local TwentyFour = script.TwentyFour
local DayMusic = script.DayMusic

--VARIABLES
local Lighting = game.Lighting
local Night = false
local Day = true

--MAIN SCRIPT
Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
	if Lighting.ClockTime < 18 and Lighting.ClockTime >= 6.18 and Night == false then
		print("Night Time!")
		Day = false
		Night = true
		DayMusic:Stop()
		wait(5)
		local NightMusic = math.random(1,3)
		if NightMusic == 1 then
			Kano:Play()
		elseif NightMusic == 2 then
			Snowfall:Play()
		elseif NightMusic == 3 then
			TwentyFour:Play()
		end
	elseif Lighting.ClockTime >= 18 and Lighting.ClockTime < 6.18 and Day == false then
		print("Day Time!")
		Day = true
		Night = false
		Kano:Stop()
		Snowfall:Stop()
		TwentyFour:Stop()
		wait(5)
		DayMusic:Play()
	end
end)
1 Like

This works… Sort of.
The music changes when it reaches nighttime, yes. But when it is daytime, the music doesn’t seem to change?

Probably because of these lines:
if Lighting.ClockTime < 18 and Lighting.ClockTime >= 6.18 and Night == false then
print(“Night Time!”)

elseif Lighting.ClockTime >= 18 and Lighting.ClockTime < 6.18 and Day == false then
print(“Day Time!”)

Seeing as >= 18 is 6pm (night time) and < 6.18 is dawn. You have your statement backwards.

2 Likes

Whoops, heres the new script

--SOUNDS
local Kano = script.Kano
local Snowfall = script.Snowfall
local TwentyFour = script.TwentyFour
local DayMusic = script.DayMusic

--VARIABLES
local Lighting = game.Lighting
local Night = false
local Day = true

--MAIN SCRIPT
Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
	if (Lighting.ClockTime >= 18 or Lighting.ClockTime < 6.18) and Night == false then
		print("Night Time!")
		Day = false
		Night = true
		DayMusic:Stop()
		wait(5)
		local NightMusic = math.random(1,3)
		if NightMusic == 1 then
			Kano:Play()
		elseif NightMusic == 2 then
			Snowfall:Play()
		elseif NightMusic == 3 then
			TwentyFour:Play()
		end
	elseif Lighting.ClockTime < 18 and Lighting.ClockTime >= 6.18 and Day == false then
		print("Day Time!")
		Day = true
		Night = false
		Kano:Stop()
		Snowfall:Stop()
		TwentyFour:Stop()
		wait(5)
		DayMusic:Play()
	end
end)
1 Like

Alright, your script works! Thank you!

1 Like

You’re welcome.
(and a few more characters)