In short, I’m making my own sound module based on 3DAudioSystem module for my future projects and better sound manipulating. But the module somehow works very strange
WARNING!! LOUD SOUNDS Here’s the video:
And here’s the code:
--// Services
local replicatedStorage = game:GetService("ReplicatedStorage")
local runService = game:GetService("RunService")
--// Folders
local modules = replicatedStorage:WaitForChild("Modules")
--// Assets
local camera : Camera = workspace.CurrentCamera
--// Modules
local settings = require(script.settings)
local trove = require(modules.Libraries.trove)
local dicitionaryExpansion = require(modules.Libraries.dicitionaryExpansion)
--// Sound Container
local soundContainer = Instance.new("Part")
soundContainer.Name = "SoundContainer"
soundContainer.CFrame = CFrame.new()
soundContainer.Anchored = true
soundContainer.CanCollide = false
soundContainer.CanQuery = false
soundContainer.CanTouch = false
soundContainer.Massless = true
soundContainer.Transparency = 1
soundContainer.Parent = camera
local oldSound_Container = Instance.new("Folder")
oldSound_Container.Name = "OldSound_Container"
oldSound_Container.Parent = replicatedStorage
--// Start Module
local system = {}
local currentSounds = {}
--// Module Functions
-- sanityCheck
system.sanityCheck = function(sound : Sound)
if not sound.Archivable then return nil end
return true
end
-- registerSound
system.registerSound = function(sound : Sound)
-- Prepare Custom Sound
local self = {}
local parent = sound.Parent
local emitter = Instance.new("Attachment")
emitter.Name = "emitter"
emitter.Parent = soundContainer
local soundObject = sound:Clone()
soundObject.Name = "Sound"
soundObject.Parent = emitter
local Equalizer = Instance.new("EqualizerSoundEffect")
Equalizer.Name = "EarSimulation"
Equalizer.LowGain = 0
Equalizer.MidGain = 0
Equalizer.HighGain = 0
Equalizer.Parent = soundObject
local soundTrove = trove.new()
soundTrove:Add(emitter)
soundTrove:Add(sound)
local parentType = typeof(parent)
if parent == "Vector3" then
emitter.WorldPosition = parent
elseif parentType == "CFrame" then
emitter.WorldCFrame = parent
else
emitter.WorldCFrame = parent.CFrame
soundTrove:Connect(runService.Heartbeat, function(deltaTime: number)
emitter.WorldCFrame = parent.CFrame
end)
end
soundTrove:Connect(sound.Ended, function(soundId: string)
if not sound.Looped then
self.destroy()
end
end)
soundTrove:Connect(sound:GetPropertyChangedSignal("Volume"), function(...: any)
soundObject.Volume = sound.Volume
end)
-- Make Table
self.trove = soundTrove
self.sound = soundObject
self.emitter = emitter
self.destroy = function()
currentSounds[sound] = nil
self.trove:Destroy()
end
currentSounds[sound] = self
-- Prepare Old
sound.Parent = oldSound_Container
-- return
return self
end
-- initialize
system.initialize = function()
runService:BindToRenderStep("audioSystem", 1, function()
-- optimization
if settings.limit and dicitionaryExpansion.length(currentSounds) >= settings.limit then
local lastSound = dicitionaryExpansion.find(currentSounds, dicitionaryExpansion.length(currentSounds))
if lastSound then
lastSound.destroy()
end
end
-- sounds
for _, soundMeta in currentSounds do
local facing = camera.CFrame
local vector = (soundMeta.sound.emitter.Position - camera.CFrame.Position).Unit
facing = Vector3.new(facing.X, 0, facing.Z)
vector = Vector3.new(vector.X, 0, vector.Z)
local angle = math.acos(Vector3.new().Dot(facing, vector) / (facing.Magnitude * vector.Magnitude))
soundMeta.sound.emitter.EarSimulation.HighGain = -(25 * ((angle / math.pi)^2))
end
-- test
print(dicitionaryExpansion.length(currentSounds))
end)
-- Existed Sounds
for _, object : Instance in workspace:GetDescendants() do
if object:IsA("Sound") and system.sanityCheck(object) then
system.registerSound(object)
end
end
-- New Sound
workspace.DescendantAdded:Connect(function(object : Instance)
if object:IsA("Sound") and system.sanityCheck(object) then
system.registerSound(object)
end
end)
end
--// return
return system
Maybe I’ve done the most common mistake or something, but I’m really tired rn cause haven’t slept for long time
Thank you in advance ![]()