Trying to make a volume smoothly increasing volume

Hello, I’m trying to make a script volumes up a sound, but really smooth, I tried with TweenService, but it didn’t worked.

		for i = 0,1,0.1 do
			script.sound.Volume = script.sound.Volume + i
wait(0.1)
		end

--Attempt with tweenservice


local TweenService = game:GetService("TweenService")
TweenService:Create(script.sound, TweenInfo.new(1), {Volume = 1})

1 Like

I am generally confused about why do you try to use TweenService to increase the volume of a sound. You can simply just run the code you entered above without TweenService. Here is an example:

for 1,10 do
   script.sound.Volume = script.sound.Volume + 0.1
   wait(0.5)
end
4 Likes

Yes, I was trying to know, as tweenservice works for a lot of stuff, like, fading out and in, I was just trying to know if it could work for this! Seems like no.

As I know TweenService is mostly used for Workspace object manipulation. For example to make an object to move in different axises etc. I don’t think sound decreasing and increasing might be possible with TweenService at all. The types of properties you can Tween with TweenService are:

  • number
  • bool
  • CFrame
  • Rect
  • Color3
  • UDim
  • UDim2
  • Vector2
  • Vector2int16
  • Vector3
1 Like

You need to play the tween in order it for to work.
The fixed code would be as follows:

local TweenService = game:GetService("TweenService")
local Tween_Info = TweenInfo.new(1)
local Tween_Goal = {Volume = 1}

local Sound = script.sound

local Tween = TweenService:Create(Sound, Tween_Info, Tween_Goal)
Tween:Play()
6 Likes

Volume is a number property, therefore it should work completely fine with TweenService.

Yes I did it, but it doesn’t works I think

Please read my reply again, I clearly state you need to play the tween in order it for to work.
The original code sample you gave us, shows you not playing it. Only creating it.

Here’s a quick tween function I scripted:

function Tween(Object, Time, Customization)
	game:GetService("TweenService"):Create(Object, TweenInfo.new(Time), Customization):Play()
end

How you would use it to tween sound:

Tween(script.sound, 1, {Volume = 1});
3 Likes

Try using a loop

for i=1,10 do
   script.Parent.Volume = script.Parent.Volume + i
   wait(0.5)
end
1 Like

You can also use a Lerp function

local Rs = game:GetService("RunService")
local Sound = script.Sound

function lerp(a,b,t)
	return a + (b-a)*t
end

function TurnUpMusic(Volume)
	for i = 1, 101, 1 do
		Rs.Heartbeat:Wait(2)
		local Value = lerp(Sound.Volume, Volume, (i-1)/100)
		Sound.Volume = Value
	end
end
3 Likes