You can write your topic however you want, but you need to answer these questions:
-
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.
A while back, I wrote a similar thread, but it’s now obsolete, mainly because the original method I used was both unsecure and somehow broke during development (the sound would audibly play on a loop, non-stop, despite TweenService). I believe my new method works with FilteringEnabled and it also uses RemoteEvents. Maybe it’s better and less prone to the aforementioned bug?
- What is the issue?
The output yields an error that says “Unable to cast value to Object”, and the horn sound never plays because of it. I’m not sure what’s going on with the code, so I’ll paste the script and Explorer layout below.
ReplicatedStorage = game:GetService("ReplicatedStorage")
TweenService = game:GetService("TweenService")
local hornEvent = ReplicatedStorage.HornEvent
local horn = script.Parent:WaitForChild("Horn")
local sound = horn.Horn
local soundLength = sound.TimeLength
local currentTween
sound.Volume = 0
sound.Looped = true
sound:Play()
local function onHornFired(player, sound)
if sound == true then
--horn.Horn:Play()
if currentTween then
currentTween:Pause()
end
currentTween = TweenService:Create(sound, TweenInfo.new(soundLength), {Volume = 1})
currentTween:Play()
end
if sound == false then
--horn.Horn:Stop()
if currentTween then
currentTween:Pause()
end
currentTween = TweenService:Create(sound, TweenInfo.new(soundLength), {Volume = 0})
currentTween:Play()
end
end
hornEvent.OnServerEvent:Connect(onHornFired)
The highlighted script in the Explorer is the one above.