Music volume not fading?

So in my script:

local tweenService = game:GetService("TweenService")

local intro = workspace["Chocoblox Factory Tycoon Intro"]
local music = workspace["Chocoblox Factory Tycoon Spring"]

script.Parent.Parent.Parent.Enabled = true
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All,false)

local length = intro.TimeLength - 1.5

local musicFadeTimeMinus = 2.25
local musicFadeTimeWait = length - musicFadeTimeMinus

local tweenInfo = TweenInfo.new(length,Enum.EasingStyle.Linear)

local function fadeOut()
	for i = 0, 1, 0.01 do
		script.Parent.Parent.BackgroundTransparency = i
		script.Parent.ImageTransparency = i
		wait(0.001)
	end
	wait(0.5)
	script.Parent.Parent.Parent:Destroy()
end

local function fadeMusic()
	print("START - FADING MUSIC")
	for i = 0.5, 0, 0.01 do
		intro.Volume = i
		print(i)
		wait(0.05)
	end
	print("END - FADING MUSIC")
end

wait(1)
local plr = game.Players.LocalPlayer
if plr then
	intro:Play()
end

local info = {}
info.Offset = Vector2.new(0,-1)

local tween = tweenService:Create(script.Parent.UIGradient,tweenInfo,info)
tween:Play()

wait(musicFadeTimeWait)
fadeMusic()
wait(musicFadeTimeMinus + 1)

coroutine.wrap(fadeOut)()
wait(0.5)
music:Play()
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All,true)
workspace.CurrentCamera.CameraType = Enum.CameraType.Custom

The tween plays and near the end the music volume goes down.

For some reason, the music doesn’t volume doesn’t fade.

I get the print statement:
print("FADING MUSIC")
but I don’t get the print statement:
print(i).

Why? Are there any typos that I missed?

Also the start and end print statements print at the same time (I think)

I believe you have the for loop wrong. The step needs to be negative since it starts off at a higher value than it ends at.

Try:

for i = 0.5, 0, -0.01 do
	intro.Volume = i
	print(i)
	wait(0.05)
end

The way you would use a for loop with 3 parameters would be:

for i = start, end, step
    -- code
end

This initiates i at the start value. i then increments by step repeatedly until i reaches end. If i will never reach end due to something like the step not allowing i to reach end, the loop will not run in order to avoid an infinite loop. Note that end is inclusive in the loop, so the loop will run when i == end is true.
For example:

for i = 2, 5, 0.5 do
    print(i)
end

Prints:

2
2.5
3
3.5
4
4.5
5

More info at: Introduction to Scripting | Documentation - Roblox Creator Hub

1 Like

Why would something print. You have set the end to 0. So basically the loop starts with 0.5, and then it ends at 0, with an incremenet of 0.01?? There is something weird here.

So change the 0 (end value) to something else.

Learn more (here)[Introduction to Scripting | Documentation - Roblox Creator Hub].

1 Like

You forgot to change the increment to a negative number, since you’re going down:

for i = 0.5, 0, -0.01 do
1 Like