Need help with script optimization. I want to try Parallel scripting but I don't understand how

  1. What do I want to achieve?
    I want to optimize this script I made for the future, so that my game doesn’t start lagging quickly the more stuff i add to it. I would love to try and use parallel scripting with this script cause I’ve heard it helps with performance a lot, but no matter how many time I try to read into it, I never understand how to implement it properly, in fact I don’t even know if this script would work with parallel.

  2. What is the issue?
    I think this script might have a lot of potential to become very laggy in the future the more I add objects with the tag mentioned in the collection service function.

Here is the script:

local CollectionService = game:GetService("CollectionService")
local player = game.Players.LocalPlayer


local function UpdateVolume(SFX)
	game:GetService("RunService").RenderStepped:Connect(function()
		local fadeVolumeDistance = SFX.RollOffMaxDistance -- Distance in studs where the part starts appearing
		local maxVolumeDistance = SFX.RollOffMinDistance -- Distance in studs where the part becomes fully transparent
		local VolumeGoal = SFX:GetAttribute("DefaultVolume")
		local character = player.Character or player.CharacterAdded:Wait()
		local humanoidRootPart = character and character:FindFirstChild("HumanoidRootPart")
		local Parent = SFX.Parent
		local distance
		local Volume
		
		if not character or not humanoidRootPart then
			return
		end

		if Parent:IsA("BasePart") then
			distance = (Parent.Position - humanoidRootPart.Position).Magnitude
		elseif Parent:IsA("Attachment") then
			distance = (Parent.WorldPosition - humanoidRootPart.Position).Magnitude
		end

		if distance <= maxVolumeDistance then
			Volume = 0
		else
			Volume = math.clamp((fadeVolumeDistance - distance) / (fadeVolumeDistance - maxVolumeDistance), 0, VolumeGoal) * VolumeGoal
		end

		SFX.Volume = Volume
	end)
end

for _, Sound in pairs(CollectionService:GetTagged("FixAudioFading")) do
	if Sound:IsA("Sound") then
		task.spawn(UpdateVolume, Sound)
	end
end
  1. What solutions have I tried so far?
  • Understanding parallel scripting (I did not)
  • Reading into similar issues on the devforum

This script is my attempt at fixing roblox’s broken max/minrolloffdistance. And don’t say “But it works for me” because it does not for me (when using roblox’s “volumetric audio”) so this was my solution. Since my game is already pretty not well optimized, I don’t think it would hurt to try to optimize this as most as possible.

Copy the local script to a script and make the run context of the script to local, this will allow you to place it under an Actor, now clone an actor with the script is its children and parent it to the sound on every object with sound fading tagged using for loop and collectsionservice tagadded on an seperate script.

Now you can run the function on every sound, added task.desynchronized on the code you want to multithread, often the calculation part and task.synchronized at the part that you dont want to, often setting for instance because they are not safe in multithread. And you sre probally done.
Tho move all the local on global, you are just tanking performance by setting its in a loop. The sfx can be located once you placed your actor to the sound

Quick question: whats wrong with the default roblox fadein and out that you want to create your own?