SoundPool
This was deprecated in v1.0.0
Deprecated
SoundPool is no longer maintained and is kept only for existing projects. There is no direct replacement in this library.
A pool of reusable, invisible anchored Parts each holding one Sound, for playing many short
3D sound effects (fighting-game SFX, hit sounds) without creating and destroying instances every
time. PlaySound borrows a part from the pool, points its Sound at the asset and plays it,
handing you a SoundPlayback to control it; the part goes back to the pool
when the sound ends or when you call Return.
The pooled parts live in a Folder created in workspace and named after the pool. Pooling is
done by frqstbite's object-pool package (ObjectPool.new(generator, minimum, maximum)), so
minimum parts are created up front and the pool never grows past maximum.
local SoundPool = require(path.to.SoundPool)
local hitSounds = SoundPool.new("HitSounds", 4, 32)
local function playHit(position: Vector3)
local playback = hitSounds:PlaySound(9118823101, { Volume = 0.8, Position = position })
-- In this version the config passed to PlaySound is not applied; set it explicitly:
playback:UpdateConfig({ Volume = 0.8, Position = position })
return playback
end
Depends on Sleitnick's Trove and Signal and on frqstbite's object-pool, all installed by Wally.
Credits: Trove and Signal are by sleitnick (sleitnick's RbxUtil); the pooling is object-pool by frqstbite. Wally installs all three.
Installation and guide: SoundPool package page.
Types
PooledSound
An invisible, anchored, non-collidable Part with a child Sound named "Sound". The Part is positioned for 3D playback; the Sound plays the asset.
SoundConfig
interface SoundConfig {Volume: number?--
Sound.Volume.
PlaybackSpeed: number?--
Sound.PlaybackSpeed (1 is normal speed).
TimePosition: number?--
Sound.TimePosition in seconds to start from.
Looped: boolean?--
Sound.Looped; false is applied too, only nil is skipped.
RollOffMinDistance: number?--
Sound.RollOffMinDistance.
RollOffMaxDistance: number?--
Sound.RollOffMaxDistance.
RollOffMode: Enum.RollOffMode?--
Sound.RollOffMode.
}
Optional property overrides for a playing sound, applied by SoundPlayback:UpdateConfig. Every
field is optional and only the fields you set are written; the rest keep whatever value the
pooled Sound already has from its previous use, so set everything you care about each time.
Note that SoundPool:PlaySound accepts a config but this version does not apply it; call
UpdateConfig on the returned playback.
Properties
PooledSoundsFolder
This item is read only and cannot be modified. Read OnlySoundPool.PooledSoundsFolder: FolderThe Folder in workspace (named after the pool's displayName) that holds every pooled Part.
ObjectPool
This item is read only and cannot be modified. Read OnlyThe underlying frqstbite object-pool that hands out and takes back pooled Parts.
Functions
new
SoundPool.new(displayName: string,--
Name of the workspace Folder that holds the pooled Parts.
minimum: number,--
How many pooled sounds to create up front.
maximum: number--
The most pooled sounds the pool may hold.
) → SoundPool--
The new pool.
Creates a SoundPool. A Folder named displayName is created in workspace to hold the pooled
Parts, and an object-pool is set up that generates Parts with _GenerateSound, creating
minimum of them right away and never holding more than maximum.
PlaySound
SoundPool:PlaySound(id: number | string,--
An asset id number, or a full sound id string.
) → SoundPlayback--
Handle to the playing sound.
Borrows a pooled sound, sets its SoundId and starts playing it, returning a
SoundPlayback handle. A number id is formatted as
rbxassetid://<id>; a string is used as-is (so any rbxassetid:// string works). The
pooled Part is returned to the pool automatically when the sound ends, or when you call
Return on the handle.
CAUTION
In this version soundConfig is accepted but never applied. Call UpdateConfig on the returned
playback to set volume, position and the other properties. Pooled Sounds keep the properties
from their previous use, so always set the ones you rely on.
Errors
| Type | Description |
|---|---|
| "Sound id given is not a valid string or number" | `id` is neither a number nor a string. |
| "PooledSound is missing Sound instance." | The borrowed Part lost its child Sound (e.g. it was destroyed externally). |