Day and Night Cycle Music [HELP]

wait(5)

local Lighting = game:GetService(“Lighting”)
local Players = game:GetService(“Players”)

local songOrder = script.AmbientSounds
local daySound = songOrder:FindFirstChild(“DayTimeSounds”)
local nightSound = songOrder:FindFirstChild(“NightTimeSounds”)

local trackOrder = {daySound}
local trackOrder2 = {nightSound}
local currentSong = Instance.new(“Model”)
currentSong.Name = “CurrentSong”
currentSong.Parent = game:GetService(“ReplicatedStorage”)

local wind = script.AmbientSounds.EnvironmentalSounds.Wind:Clone()
wind.Parent = workspace.Sounds
wind.Looped = true
wind:Play()
local forest = script.AmbientSounds.EnvironmentalSounds.DayTimeForest:Clone()
forest.Parent = workspace.Sounds
forest.Looped = true
forest:Play()

local function playSong(Music)
local playingSound = currentSong:GetChildren()
for i = 1, #playingSound do
if playingSound[i]:IsA(“Sound”) then
playingSound[i]:Destroy()
end
end

local newSong = Music:Clone()
newSong.Parent = currentSong
newSong:Play()

local playerMusic = game.ServerStorage.Assets.Objects.Misc.playerMusic:Clone()

for i, Player in pairs(Players:GetPlayers()) do
	playerMusic.Parent = Player.PlayerGui
	playerMusic.MusicBox.Song.Text = Music.Name
end
print(Music.Name)
game.Debris:AddItem(playerMusic, 5.5)
if Music:FindFirstChild("Length") then
	wait(Music:FindFirstChild("Length").Value)
else
	error("The value Length could not be found.")
end

end

onTrack = 1
onTrack2 = 1
local function PLAY()
while true do
local ChosenSong = nil
if game.Lighting.ClockTime >= 6 then

		local day = trackOrder[onTrack]:GetChildren()
		if onTrack >= #trackOrder then
			onTrack = 1
		else
			onTrack = onTrack + 1
		end
		ChosenSong = day[math.random(1, #day)]
		while currentSong:FindFirstChild(ChosenSong.Name) do
			ChosenSong = day[math.random(1, #day)]
		end
	elseif game.Lighting.ClockTime >= 17 or game.Lighting.ClockTime < 6 then
		
		local night = trackOrder2[onTrack2]:GetChildren()
		if onTrack2 >= #trackOrder2 then
			onTrack2 = 1
		else
			onTrack2 = onTrack2 + 1
		end
		ChosenSong = night[math.random(1, #night)]
		while currentSong:FindFirstChild(ChosenSong.Name) do
			ChosenSong = night[math.random(1, #night)]
		end
	end
	if ChosenSong ~= nil then
		playSong(ChosenSong)
	end
	wait()
end

end

Lighting:GetPropertyChangedSignal(“ClockTime”):Connect(PLAY)

PLAY()

So basically, what I’m trying to do here is have day time music play while it being 6 to 17, but when it’s above 17 or lesser than 6, it would play night time music, but it doesn’t seem to want to work, any ideas as to why?

1 Like

Is there any errors

--Additionally here is another point of failure (I think )
currentSong.Parent = game:GetService(“ReplicatedStorage”)
-- A Parent cannot be a service

--Change to
local RepSto = game:GetService("ReplicatedStorage")
currentsong.Parent = RepSto

No errors at all, it works just fine, it’s just that when I change the time manually, it’s still playing day time music instead of night music.

2 Likes