-
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. -
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
- 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.