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