I'm having troubles with sounds causing lag

My game runs smooth for me, but there are times when it has infrequent lag spikes that last a second or two. I tried opening the microprofiler, but I’m not quite familiar with how to use it. But recently I saw that when the framerate drops and I pause and go into detailed mode I can see ‘Sound’ being a huge problem. It may not seem like a problem since keyword is infrequent, but to players with really bad devices I’m sure it’d likely crash their game resulting in player decrease.

The problem is I’m unsure on how to optimize my sounds. I’ve talked to a few others about this and they tell me to just make 1 sound that plays and don’t clone sounds. But where my issue lies is that when a player dies in my game a sound needs to be inserted into their player or a brick in order for it to play. If it was just 1 sound then it would be teleporting all around the map playing and it well… I think most would get what I’m trying to say here…

I also don’t know if I can link the game here, so I’m gonna refrain from doing that. Please help me come up with some optimization system for my sounds. I can however provide a piece of code from one of my effects when a player dies:

local module = {}

local rainbowcolors = {Color3.new(1,0,0), Color3.new(1,.5,0), Color3.new(1,1,0), Color3.new(0,1,0), Color3.new(0,.8,1), Color3.new(0,0,1), Color3.new(.5,0,1), Color3.new(.3,0,1)}
local ts = game:GetService("TweenService")

function module.die(player, character)

	local rcolor = rainbowcolors[math.random(1,#rainbowcolors)]
	
	local explosion = script.explosion:Clone()
	explosion.Parent = character.PrimaryPart
	explosion.PlaybackSpeed = 1.1 + (math.random(0,100) / 300)
	explosion.Volume = .5
	explosion:Play()
	game.Debris:AddItem(explosion, explosion.TimeLength)
	
	local yellsound = script.yellsound:Clone()
	yellsound.Parent = character.PrimaryPart
	yellsound.PlaybackSpeed = 1.1 + (math.random(0,100) / 300)
	yellsound.Volume = 1
	yellsound:Play()
	game.Debris:AddItem(yellsound, yellsound.TimeLength)

	local vfx = script.Explosion:Clone()
	vfx.CFrame = character.HumanoidRootPart.CFrame
	vfx.Anchored = true
	vfx.CanCollide = false
	vfx.CanTouch = false
	vfx.CanQuery = false
	vfx.Parent = character.HumanoidRootPart
	
	task.wait()
	
	for i,v in vfx:GetDescendants() do
		if v:IsA("ParticleEmitter") then
			if v.Name == "Custom" then
				v.Color = ColorSequence.new(rcolor)
			end
			local count = v:GetAttribute("EmitCount")
			v:Emit(count)
		end
	end
	game.Debris:AddItem(vfx, 2)
	
	local light = script.light:Clone()
	light.Color = rcolor
	light.Parent = character.HumanoidRootPart
	
	local track = ts:Create(
		light,
		TweenInfo.new(.25, Enum.EasingStyle.Back, Enum.EasingDirection.Out, 0, false, 0),
		{Brightness = 0}
	):Play()
	game.Debris:AddItem(light, .26)
	
	if character then
		character:BreakJoints()
	end
end

return module
1 Like

To try and optimize the sound for your code

1. Pooling Sounds

Instead of cloning sounds every time they are needed, you can create a pool of pre-created sound objects and reuse them. This technique is known as object pooling.

2. Using Preloaded Sounds

Load and store sounds in a centralized location and control their playback through scripting. This avoids the need to create new sound instances frequently.

3. Reducing Sound Instances

Evaluate whether all sound effects need to be 3D (spatial) sounds or if some can be global sounds. Managing fewer 3D sounds can significantly reduce overhead.

Maybe it’s because you are cloning the sound, setting the Properties, then playing it, then destroying it after it’s played.

Why not load all the sounds into the player when they spawn in. This means they are already in the player’s HumanoidRootPart and you can just :Play() them when needed.

It saves the time it takes to load the sound when you clone it, the time it takes to set the Volume Property, and if the player is already dying and being destroyed it saves you from the time it takes to destroy the sound (and the sound.TimeLength?).

1 Like

Sound Pooling

So for pooling spatial sounds would it count if I pool sounds into the players character inside of a folder and whenever the sounds are needed to be played I parent the one I want to play in the characters root?

What I mean is if I place a folder full of all the sounds into startercharacter would it count as pooling and reduce lag?

Preloaded Sounds

I’m unsure on how I would do this. I can’t see it working well for spatial sounds that need to play over eachother.

I’ll give this a try because I thought of that too

It gives you the advantage of having the sound preloaded into the player’s Part, making it spatial, and playing immediately without lagging anything.

One issue with that is for some of my effects it requires me to delete the player’s character which means I need to play a sound somewhere else so what I do is I clone a part and cframe it to the player’s character. Maybe I should instead try just creating a part for every player that joins and make that part position itself and play.

EDIT: Nvm I can just parent it into the part before destroying the character.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.