Music fadeout doesnt work right

basically, if i set my music 2 times in a row right when the volume is going up it just stays at 0 volume.
please tell me how i can improve this script because i am feeling like its NOT the best…

local music = workspace:WaitForChild("music")
local plr = game.Players.LocalPlayer

local downvoluming = false
local upvoluming = false
local DOWNVOLUMENOWEXCLAMATIONMARK = false

local curID = 0
local curpitch = 1
local curfullvol = 1
local curfadeouttime = 1
local curfadein = 1
local curbonuses = {}
local whatithastobe = 1


local muted = false

function changemusics(musicID, pitch, defaultVOL, fadeouttime, fadeintime, bonuses)
	if musicID == curID then return end
	local OURfullvol = defaultVOL
	curID = musicID
	curpitch = pitch
	curfullvol = defaultVOL
	curfadein = fadeintime
	
	-- why do i save these values instead of just using them when i have to set the music id or whatever else? so that if we fully downvolume we set the music to the latest requested music.
	
	
	print(downvoluming
		,upvoluming
		,DOWNVOLUMENOWEXCLAMATIONMARK)


	if upvoluming then
		DOWNVOLUMENOWEXCLAMATIONMARK = true
		curfadeouttime = fadeouttime
		return
	end

	if not downvoluming then
		downvoluming = true
		local fadeoutnow = curfadeouttime
		local fullvol = music.Volume
		for i = 1, fadeoutnow * 100, 1 do
			whatithastobe = fullvol * (1 - i/(fadeoutnow*100))
			if not muted then music.Volume = whatithastobe else music.Volume = 0 end
			task.wait(.01)
		end
		downvoluming = false
		curfadeouttime = fadeouttime
	else
		curfadeouttime = fadeouttime
		return
	end

	task.wait(curfadeouttime*1.65) -- so that the music doesnt instantly come back.
	
	music.TimePosition = 0
	music.SoundId = "rbxassetid://"..tostring(curID)
	music.PlaybackSpeed = curpitch


	if not upvoluming then
		upvoluming = true
		for i = 1, curfadein*100, 1 do
			if DOWNVOLUMENOWEXCLAMATIONMARK == true then -- emergency fade out if we have to switch music when its getting upvolumed.
				local musnow = music.Volume
				DOWNVOLUMENOWEXCLAMATIONMARK = false
				downvoluming = true
				for i = 1, (musnow/OURfullvol)*100, 1 do
					whatithastobe = musnow * (1 - i/((musnow/OURfullvol)*100))
					if not muted then music.Volume = whatithastobe else music.Volume = 0 end
					task.wait(.01)
				end
				downvoluming = false
				break
			end
			whatithastobe = curfullvol * (i/(curfadein*100))
			if not muted then music.Volume = whatithastobe else music.Volume = 0 end
			task.wait(.01)
		end
		upvoluming = false
	end
end


game.ReplicatedStorage.remoteevents.setmusic.OnClientEvent:Connect(changemusics)
game.ReplicatedStorage.remoteevents.setmusicCLIENT.Event:Connect(changemusics)

if someone doesnt understand what i mean i could clarify it tommorow, very sorry but i have to go right now…

1 Like

When you are using an instance like Animation or Sound, try creating it instead of changing the ID via script.

sure, i could do that, but im not exactly sure if itll fully fix my issue. i think theres something wrong with my if statements, tbh.

I’ll reproduce your script and get back to you with something.

2 Likes

Okay, from what I’ve observed, the “muted” state is never changed through this script, so what for me may be causing the volume to be constantly 0 is this variable: local fullvol = music.Volume

Shouldn’t this reset the volume to the default value? Because your for loop always goes up to 100, implying that the song should always start with the volume fixed at a value.

1 Like

Hello!

If the sound is in a part and you want to fade it in close to part or fade it out far from the part you can just set the RollOffMode property to “InverseTapered”

Have a nice time!

One more point, which I don’t know if you know, but… did you know that Tween can change the volume of the Sound instance? The effect is excellent if you configure it well, I was testing it yesterday. It’s worth testing, you might like it.

that’s right, you can use tween service to fade out music. You can use it like this:
game:GetService("TweenService"):Create(sound, TweenInfo(--[[your settings(time of fadeout, easing style, easing direction, etc.)]]--), {Volume = 0}):Play() . The volume is a property of the sound. You can add more properties in the table, like PlaybackSpeed or RollOffMaxDistance or idk you choose.

1 Like

It’s TweenInfo.new(), not TweenInfo().

you’re right, sorry about that lol

And also in the closing comment bracket, do not use the – as it gives a script error.

no no, it is changed just outside of the script that ive shown here. dont mind it, please.

yeah i know but what if, when im upvoluming, the server requests me to change my music again and i have to stop the tween? oh wait on second thoght i could indeed do that with tween:cancel but it will be a PAIN to bring over the reference to the tween just to cancel it…