soundUtil
A utility module for working with sounds.
soundUtil.registerSoundHeartbeatCallback(someSound, function(sound, deltaTime)
if not sound.Playing then
sound.Volume = 0
sound:Play()
end
-- Lerp the volume to 1:
sound.Volume += (1 - sound.Volume) * deltaTime * 2
end)
soundUtil.registerSoundCleanupCallback(someSound, function(sound, deltaTime)
-- Lerp the volume to 0:
sound.Volume += (0 - sound.Volume) * deltaTime * 2
if sound.Volume < 1e-3 then
-- We've fully lerped the volume to 0, let's stop the sound and finish
-- up cleanup!
sound:Stop()
return false
end
return true
end)
soundUtil.playSound(someSound)
Functions
playSoundCloneAndCleanup
Plays a cloned version of the sound and destroys it when it has ended.
stopAll
soundUtil.
stopAll
(
) →
(
)
Stops all playing registered ounds via soundUtil.stopSound.
playSound
Plays the given sound instance, if the sound instance has a heartbeat callback registered for it, then the sound instance will not be played by this method.
soundUtil.registerSoundHeartbeatCallback(someSound, function(sound, deltaTime)
if not sound.Playing then
sound.Volume = 0
sound:Play()
end
-- Lerp the volume to 1:
sound.Volume += (2 - sound.Volume) * deltaTime
end)
soundUtil.playSound(someSound)
playSoundIn3DOnce
Plays the given sound at the specified given 3D position or instance. If the sound instance has a heartbeat callback registered for it, then the sound instance will not be played by this method. Once the sound has ended, it'll be destroyed.
-- Usecase: lightning bolt sounds!
local lightningBoltSound = ...
local generatedLightningBoltModel = ...
soundUtil.playSound3D(lightningBoltSound, generatedLightningBoltModel.PrimaryPart)
NOTE
Ideally you should always generally pass in a cloned version of the sound instance so you can reuse it.
-- BAD:
soundUtil.playSoundIn3DOnce(someStorage.someRandomSound, workspace.Baseplate)
soundUtil.playSoundIn3DOnce(someStorage.someRandomSound, workspace.ZombiePart) -- PANIC: `someStorage.someRandomSound` does not exist anymore!
-- GOOD:
oundUtil.playSoundIn3DOnce(someStorage.someRandomSound:Clone(), workspace.Baseplate)
soundUtil.playSoundIn3DOnce(someStorage.someRandomSound:Clone(), workspace.ZombiePart)
stopSound
Stops the given sound instance.
registerSoundCleanupCallback
soundUtil.
registerSoundCleanupCallback
(
) →
(
)
Registers the given callback as a cleanup callback for the given sound instance. The callback will be
called every Heartbeat when the
sound is stopped via soundUtil.stopSound, for as long as it doesn't return false
(to complete cleanup).
local someSound = ...
soundUtil.registerSoundCleanupCallback(someSound, function(someSound, deltaTime)
-- Lerp the volume to 0:
someSound.Volume += (0 - someSound.Volume) * deltaTime
if someSound.Volume < 1e-3 then
-- We've fully lerped the volume to 0, let's stop the sound and finish
-- up cleanup!
someSound:Stop()
return false
end
return true
end)
soundUtil.playSound(someSound)
task.wait(2)
someSoundUtil.stopSound(someSound)
NOTE
The cleanupPredicate
will no longer run if the same sound instance is suddenly played again.
registerSoundHeartbeatCallback
soundUtil.
registerSoundHeartbeatCallback
(
) →
(
)
Registers the given callback as a heartbeat callback for the given sound instance. It will be called every Heartbeat for as long as the sound is playing (sound will be in "playing" state if it has been played by soundUtil.playSound and has not finished / stopped yet).
soundUtil.registerSoundHeartbeatCallback(someSound, function(sound, deltaTime)
if not sound.Playing then
sound.Volume = 0
sound:Play()
end
-- Lerp the volume to 2:
sound.Volume += (2 - sound.Volume) * deltaTime
end)
soundUtil.playSound(someSound)