Help with playing Music on change of ClockTime

I’ve been having a headache with this one and I’ve been fixing it FOR DAYS. Can anyone help me out and possibly detect why is it playing music from another table?

I basically have 3 Tables of different Music for separate time of the day in game, and whenever a audio finishes, it’ll play- lets say an Audio from a different table.

Am I doing something wrong?? (ps, let me know more if you need help with telling the code, but for now, this is what I’m going to show)

function musicPlay(dayValue: string) -- This basically controls the Music Player.
	local currentDayValue = dayValue

	while true do	
		task.wait(0.5)
		local CurrentId = Lib.AudioAssets.AmbientAudio[tostring(currentDayValue)][math.random(1, #Lib.AudioAssets.AmbientAudio[tostring(currentDayValue)])]
		if CurrentId == nil then return end

		local LastId = WS.Sounds.Ambient.SoundId
		task.wait(2)
		WS.Sounds.Ambient.SoundId = "http://www.roblox.com/asset?id="..CurrentId
		while WS.Sounds.Ambient.SoundId == LastId do
			CurrentId = Lib.AudioAssets.AmbientAudio[tostring(currentDayValue)][math.random(1, #Lib.AudioAssets.AmbientAudio[tostring(currentDayValue)])]
			WS.Sounds.Ambient.SoundId = "http://www.roblox.com/asset?id="..CurrentId
		end

		WS.Sounds.Ambient:Play()
		if WS.Sounds.BellPlaying == true then
			TweenModule.Tween(WS.Sounds.Ambient, TweenInfo.new(1), {Volume = 0.1})		
		else
			TweenModule.Tween(WS.Sounds.Ambient, TweenInfo.new(1), {Volume = 0.6})
		end

		WS.Sounds.Ambient.Ended:Wait()
		WS.Sounds.AmbientPlaying = false
	end
end

-- I know this Part can be minimized and turned into a much more simpler command-
-- But right now, I really needed help with figuring out why its playing different music from a different Table.
function Audio.musicManager(Value) -- This sends the info of which table will be accessed.
	local lastValue = Value
	if lastValue == "Morning" then
		if WS.Sounds.BellPlaying == false then
			WS.Sounds.BellPlaying = true
			tweenBell()
		end

		if WS.Sounds.AmbientPlaying == true then
			WS.Sounds.AmbientPlaying = false
			tweenOut("Morning")
		else
			WS.Sounds.AmbientPlaying = true
			musicPlay("Morning")
		end
	elseif lastValue == "Dinner" then
		if WS.Sounds.BellPlaying == false then
			WS.Sounds.BellPlaying = true
			tweenBell()
		end

		if WS.Sounds.AmbientPlaying == true then
			WS.Sounds.AmbientPlaying = false
			tweenOut("Dinner")
		else
			WS.Sounds.AmbientPlaying = true
			musicPlay("Dinner")
		end
	elseif lastValue == "Curfew" then
		if WS.Sounds.BellPlaying == false then
			WS.Sounds.BellPlaying = true
			tweenBell()
		end

		if WS.Sounds.AmbientPlaying == true then
			WS.Sounds.AmbientPlaying = false
			tweenOut("Curfew")
		else
			WS.Sounds.AmbientPlaying = true
			musicPlay("Curfew")
		end
	end
end

Just a guess but you could try waiting for the sound to load?

if not WS.Sounds.Ambient.IsLoaded then
    WS.Sounds.Ambient.Loaded:Wait()
end
WS.Sounds.Ambient:Play()
1 Like

hi there, I’ll try to do check this and see if it works, thanks :smiley: