Hi, so I’m creating a game with a weapon system that utilises realistic and positional audio.
It does this by receiving remotes
(client - server - client)
with positioning of where the gunshot was, and where it should go (for projectile and sound calculations)
however, ive come across an issue. A real pain in the backside. When creating sounds, no stutter. Deleting the sound is a different story.
For sounds, an attachment is created where the gunshot was (parented to terrain), and then a sound created under said attachment which plays. when the sound has finished the attachment destroys, taking the sound along with it.
When the attachment destroys, a huge lag spike ensues, the micro profiler labels this as “Render job”
Which is strange considering the attachment is invisible, and has no collisions.
The ONLY solution I have found is literally deleting the entire map, but as you might’ve guessed, I can not do that.
fireRemote.OnClientEvent:Connect(function(playerr, currentWeapon, currentSlot, Handle, bulletCaliber)
local plrchar = workspace:FindFirstChild(playerr.Name)
local HRPP = plrchar:WaitForChild("HumanoidRootPart")
local hrppPOS = HRPP.Position
local distance = (hrppPOS - HRP.Position).Magnitude
local plrArea = playerr:WaitForChild("TypeOfArea").Value
local distVar = "VClose"
if distance >= 200 and distance <= 500 then
distVar = "Close"
elseif distance >= 500 then
distVar = "Medium"
end
local sounds = weaponAudios[currentSlot][currentWeapon][distVar]:GetChildren()
local randomSound = sounds[math.random(1, #sounds)]:Clone()
--local soundpart = plrchar.HumanoidRootPart
local soundpart = Instance.new("Attachment")
soundpart.Parent = workspace.Globalpart
soundpart.WorldPosition = hrppPOS
local eq = Instance.new("EqualizerSoundEffect")
eq.HighGain = distance * -0.04
eq.MidGain = distance * -0.04
eq.LowGain = distance / 3
eq.Parent = randomSound
randomSound.Parent = soundpart
if Reflectors:FindFirstChild(plrArea) then
local reflectorAudios = Reflectors[plrArea][distVar][bulletCaliber]:GetChildren()
local randomReflector = reflectorAudios[math.random(1, #reflectorAudios)]:Clone()
if distVar == "VClose" then
if plrArea == "Small" then
randomReflector.Parent = soundpart
else
randomReflector.Parent = camera
end
else
randomReflector.Parent = soundpart
end
task.spawn(PlayAudioOnSepThread, randomSound, randomReflector, localplayer:DistanceFromCharacter(HRPP.Position) / speedofsound)
DebrisService:AddItem(soundpart, randomReflector.TimeLength)
end
end)
Nothing super advanced, everything from audio effects, animations and tweens are all handled on the client. Ive tried using :Destroy() with a yield but i tend to avoid yielding since its bad practise, so i use debrisservice
I’m honestly unsure, maybe do to the firerate of the gun or wtv, the attachments spawn and delete very fast along with the 2 other instances attached to it.
Maybe, you can move it very far away and delete them gradually or use something like PartCache module?
Okay so, turns out the issue was due to mesh-parts with a high concentration of bones, which I used for my ray-cast displaced road system. Obviously it was overloading the current instance count in some way (theres literally thousands of bones total) and causing the stutter. Removing them completely fixes the stutter and even makes other cloning and instance.new functions run 1000x faster.