Since playing a sound twice on Roblox doesn’t overlap, but rather stops the sound and makes it restart, over like two years I’ve made it a habit to clone a short sound that could play twice, and delete it soon after, that way the sound can overlap. I recently made a relatively big game and I noticed that in some videos, some sounds don’t play at all. Is that because of this little technique I do? is this bad for performance? Is it entirely suboptimal and I’m stupid? Oh and I do want to clarify I only do this on the client.
performance impact wont be enough to care, you can object pool them if you want though
this is the code i usually use for playing sounds, a little outdated but yk
function PlaySoundAtPosition(SoundObj:Sound, Position:Vector3, sound_properties:{PlaybackSpeed:number?}) : Sound
local SoundPart = Instance.new("Part", workspace.Cleanables) -- SET TO WHERE YOUR SOUND PARTS SPAWN!!
SoundPart.Position = Position
SoundPart.Anchored = true
SoundPart.CanCollide = false
SoundPart.CanTouch = false
SoundPart.Transparency = 1
game.Debris:AddItem(SoundPart,30) -- backup timer
local SoundObj = SoundObj:Clone()
SoundObj.Parent = SoundPart
if sound_properties then
for propertyName, propertyValue in sound_properties do
SoundObj[propertyName] = propertyValue
end
end
SoundObj:Resume()
game.Debris:AddItem(SoundPart, SoundObj.TimeLength)
return SoundObj
end
shouldnt be too hard to see how you could make PlaySoundAtRootPart or PlaySoundAtObject variants
Probably bad for performance. If you want sounds to overlap, just create a small pool of 5-10 sounds, then just play whatever sound that isn’t playing when you want to play another sound.
This is easily solvable if you define a constant N of how many sounds can play at once. Then you only need N sound instances that always exist and you just choose to play a sound on the one that is available / doesn’t play a sound. If there’re no such instances, then you use the one that started playing a sound the longer back in time or a sound that doesn’t matter much (for example a sound effect).
Ideally you use some kind of module that marks the available sounds and does things on the background, instead of relying on loops and property detective work to figure out the available ones.
Sorry for necroposting, and thanks for the resource.
I wanted to discourage usage of Debris when possible, since it’s slower than the equivalent using task.delay
task.delay(30, SoundObj.Destroy(), SoundObj)
In general, Debris is much older and slower than the new task library.
Is this neglibible? In this case, probably. But if you’re juggling many sounds it will add up!