What's wrong with TweenService on my horn sound script?

  1. What do you want to achieve?
    I’m trying to make a horn sound with a smooth fade-in and fade-out. The horn sound would fade in (volume increase) when the “h” key is pressed, and it would fade out when the same key is released.

  2. What is the issue?
    When I try the code below, no sound plays at all. I looked at the TweenService documentation, but I’m still clueless.

  3. What solutions have you tried so far?
    None. The horn sounds pretty ugly when it’s just enabled and disabled abruptly, so I could really use a solution that won’t cause a memory leak or something.

mouse.KeyDown:connect(function(key)
	if key == "h" then 
		tweenService:Create(Value,TweenInfo.new(0.5), {Volume = 1}):Play()
	end
end)
mouse.KeyUp:connect(function(key)
	if key == "h" then 
		tweenService:Create(Value,TweenInfo.new(0.5), {Volume = 0}):Play()
	end
end)

“Value” is actually the Sound object. My system uses a different approach than just fetching a Sound object right off the bat.

Tweening is still pretty new to me. I’m not asking for anyone to make code for me, I just need help with getting my horn sound to play with smooth transitions. I could use a “for” statement, but that isn’t as efficient as tweening. I seek no interest in changing the volume of the sound numerous times with a wait() in between, as that’s even more inefficient.

I’d also look to point out that I know for a fact that the non-sound-related code, like the keybind input and other things, are not the problem. The sound does play without tweening, but again, it sounds really ugly when it just goes on and off without any kind of fading. The code does fetch TweenService, but I excluded it from the extraction above.

1 Like

I believe you are confusing with Sound:Play() and tween service :Play(), these two are different one plays the sound which is missing.

Here is how I believe it should work:

local Value = Instance.new("Sound") -- sound
local soundLength = Value.TimeLength
Value.Volume = 0 --initially at zero 
Value.Looped = true --Horn sound should be loopable in order to continuosly spam the horn
Value:Play()--is already playing all we have to do is adjust the volume

local currentTween

mouse.KeyDown:connect(function(key)
	if key == "h" then 
		if currentTween then
			currentTween:Pause()
		end
		currentTween = tweenService:Create(Value,TweenInfo.new(soundLength), {Volume = 1})
		currentTween:Play()
	end
end)
mouse.KeyUp:connect(function(key)
	if key == "h" then 
		if currentTween then
			currentTween:Pause()
		end
		currentTween = tweenService:Create(Value,TweenInfo.new(soundLength), {Volume = 0})
		currentTween:Play()
	end
end)
2 Likes

This method actually worked! I did tweak the first few lines though, as well as swapped out the soundLength variable with a number (the horn sound only needs to be half a second or so), as the Sound object already exists and is looped by default. You’re right about the :Play() being mixed up between TweenService’s and Sound’s.

Thanks a bunch!

1 Like