I made this simple little sound module to play your sounds efficiently.
It was made in like 10 minutes so dont get mad if there are bugs ill probably improve it later
@Methods
CreateSoundPart: Creates a part fit for playing a sound.
PlaySound: Plays sound. Takes in Lifetime, Parent and sound properties.
--//Services
local DebrisService = game:GetService("Debris")
local SoundService = game:GetService("SoundService")
local RunService = game:GetService("RunService")
--//Variables
local WorkspaceSoundFolder: Folder = Instance.new("Folder")
WorkspaceSoundFolder.Parent = workspace
WorkspaceSoundFolder.Name = "Sounds"
--//Module
local SoundModule = {}
--//Methods
function SoundModule:CreateSoundPart(Lifetime, Parent)
-- Create Sound part
local SoundPart: BasePart = Instance.new("Part")
-- Lock position in real time
SoundPart.Anchored = true
-- Exclude from raycasts and other operations
SoundPart.CanCollide = false
SoundPart.CanQuery = false
SoundPart.CanTouch = false
-- Hide part from client
SoundPart.Transparency = 1
-- Sort Sound
if Parent then
SoundPart.Parent = Parent
else
SoundPart.Parent = WorkspaceSoundFolder
end
if Lifetime then
DebrisService:AddItem(SoundPart, Lifetime)
end
return SoundPart
end
function SoundModule:PlaySound(InputSound, Lifetime, Parent, Adjustments)
-- Check if "Sound" IsA Sound
if not InputSound:IsA("Sound") then return end
-- Check for repeated signals
if InputSound ~= InputSound then return end
-- Establish sound instance
local Sound: Sound = InputSound:Clone()
-- Create SoundPart variable
local SoundPart: BasePart
local Connection = RunService.RenderStepped
-- Take in Parent variable if
if Parent then
SoundPart = SoundModule:CreateSoundPart(Lifetime, Parent)
else
SoundPart = SoundModule:CreateSoundPart(Lifetime)
end
-- Take in Lifetime variable if
if Lifetime then
SoundPart = SoundModule:CreateSoundPart(Lifetime, Parent)
else
SoundPart = SoundModule:CreateSoundPart(nil, Parent)
end
if not Lifetime or Parent then
SoundPart = SoundModule:CreateSoundPart()
end
-- Parent sound to SoundPart to initiate SoundService API
Sound.Parent = SoundPart
-- Set Adjustments to local sound
if Adjustments.TimePosition then
Sound.TimePosition = Adjustments.TimePosition
end
if Adjustments.PlaybackRegion then
Sound.PlaybackRegion = Adjustments.PlaybackRegion
end
if Adjustments.PlaybackSpeed then
Sound.PlaybackSpeed = Adjustments.PlaybackSpeed
end
if Adjustments.RollOffMode then
Sound.RollOffMode = Adjustments.RollOffMode
end
if Adjustments.RollOffMaxDistance then
Sound.RollOffMaxDistance = Adjustments.RollOffMaxDistance
end
if Adjustments.RollOffMinDistance then
Sound.RollOffMinDistance = Adjustments.RollOffMinDistance
end
if Adjustments.Looped then
Sound.Looped = Adjustments.Looped
end
if Adjustments.Volume then
Sound.Volume = Adjustments.Volume
end
-- Return boolean OnIs_______
if Adjustments.ReturnOnIsPlaying then
local Status: boolean
Connection:Connect(function()
if Status == true then return end
if Sound.IsPlaying == true then
Status = true
Connection:Disconnect()
return true
else
Status = false
return false
end
end)
end
if Adjustments.ReturnOnIsLoaded then
local Status: boolean
Connection:Connect(function()
if Status == true then return end
if Sound.IsPlaying == true then
Status = true
Connection:Disconnect()
return true
else
Status = false
return false
end
end)
end
if Adjustments.ReturnOnIsPaused then
local Status: boolean
Connection:Connect(function()
if Status == true then return end
if Sound.IsPlaying == true then
Status = true
Connection:Disconnect()
return true
else
Status = false
return false
end
end)
end
-- Start sound
Sound.Playing = true
end
return SoundModule