Stuttering, lag, when destroying parts/sound objects

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.

https://cdn.discordapp.com/attachments/878286368005050388/1280352819739754521/2024-09-03_01-26-09.mp4?ex=66d7c4fc&is=66d6737c&hm=00435f206988e5684025dda797346bacab962c5fdf69c9c1f3838e056003eeb5&

^ video above documenting the issue

1 Like

May we see the script that does this?
also sounds, animations, tweening, etc… should be handled by clients to reduce server workload.

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?

1 Like

Yeah i suppose i could try that, thanks anyway :+1:

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.

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