Hello! This is my first forum post so apologies if anything here is done improperly. ^^
Basically, I’m making a game, and I’m attempting to make a Module to make playing sounds more convenient and easy!
As an example, If I wanna just play a sound with the module (at least the module how it’s MEANT to work) then I would need to just do SoundModule(SoundIdHere, SoundLocation, {PropertiesTable} ) and it’d play the sound super duper easy!
…However, I’m currently facing an issue where Sounds either Don’t make any Audio happen, they Stack, or they work perfectly fine for seemingly no reason?
In the clip below, there’s meant to be a Swing sound, and a Hit sound for my weapon.
However, as discussed above, sometimes the sounds don’t play, or sometimes they stack!
I have zero idea why this is happening.
I’ve tried several solutions I’ve seen on the forums (Using SoundService:PlayLocalSound(), Waiting till the sound is Loaded, SEVERAL other things, and NOTHING seems to work.)
I’m at my wits end, and would really like this module to work.
It’s why I’m making this post, cause I genuinely have no ideas left.
(It’s gotten bad enough I’ve asked the Roblox Assistant for help hoping for some miracle to happen and they’ll magically see some issue with my code…)
NOTE: This doesn’t happen with the dash for some reason! Also the sounds used in the video are PLACEHOLDERS!
I’ve left all of the code below, in just some hope that someone will point out some obvious flaw that I never could’ve seen.
For the record, I’m new to using RunService, and not entirely familiar with using Coroutines.
I’m trying, and failure is apart of the learning process, but god is it frustrating when NOTHING I’ve found works.
--[ This module is for sounds to be done on the Client. ]
--[ Containers ]--
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ReplicatedRemotes = ReplicatedStorage.Remotes
local Assets = ReplicatedStorage.Assets
local ReplicatedSounds = Assets.Audio
--[ Services ]--
local Debris = game:GetService("Debris")
local Players = game:GetService("Players")
local SoundService = game:GetService("SoundService")
local RunService = game:GetService("RunService")
--[ Instances ]--
local PlaySoundRemote = ReplicatedRemotes["VFX/SFX"].SFX.PlaySound
--[ Tables ]--
-- N/A
--[ Settings ]--
local MasterVolume = 3 -- Placeholder.
--[ Functions ]--
local function UpdateSoundId(SoundId)
if typeof(SoundId) == "number" then
SoundId = `rbxassetid://{SoundId}`
elseif typeof(SoundId) == "string" then
SoundId = SoundId
end
return SoundId
end
-- For the record, this is only here because i tried removing the Sound.Ended:Wait() Sound:Destroy() stuff!
local function IsSoundRegisteredInDatabase(SoundId)
for _, Sound in ReplicatedSounds:GetDescendants() do
if Sound:IsA("Sound") then
if Sound.SoundId == SoundId then
return true, Sound
end
end
end
return false
end
return function(SoundId, Location, Properties, Connections)
if not SoundId then warn("No SoundId provided.") return end
if RunService:IsClient() then
local NewSoundId = UpdateSoundId(SoundId)
local Success, SoundToPlay = IsSoundRegisteredInDatabase(NewSoundId)
if not Success then
SoundToPlay = Instance.new("Sound", ReplicatedSounds)
SoundToPlay.SoundId = NewSoundId
end
if Properties then
for Property, Value in Properties do
local Success, ErrorCode = pcall(function()
if SoundToPlay[Property] then
SoundToPlay[Property] = Value
end
end)
if not Success then
warn(`{Property} is not a valid Property of Instance {SoundToPlay}.`)
end
end
end
if not Location then
SoundToPlay.Parent = workspace:WaitForChild("Sounds")
elseif typeof(Location) == "Vector3" then
local Part = Instance.new("Part")
Part.Parent = workspace:WaitForChild("Sounds")
Part.Size = Vector3.one
Part.Position = Location
Part.Transparency = 1
Part.Anchored = true
Part.CanCollide = false
Part.CanTouch = false
Part.CanQuery = false
SoundToPlay.Parent = Part
elseif Location:IsA("BasePart") then
SoundToPlay.Parent = Location
end
SoundToPlay.Volume *= MasterVolume
-- Coroutine so .Loaded:Wait() doesn't prevent code from running.
local Coroutine = coroutine.create(function()
if not SoundToPlay.IsLoaded then
SoundToPlay.Loaded:Wait()
end
SoundToPlay:Play()
SoundToPlay.Ended:Wait()
SoundToPlay.Parent = ReplicatedSounds
end)
coroutine.resume(Coroutine)
return SoundToPlay
elseif RunService:IsServer() then
if not Properties["Player"] then
PlaySoundRemote:FireAllClients(SoundId, Location, Properties)
elseif Properties["Player"] then
local Player = Players:FindFirstChild(Properties["Player"])
table.remove(Properties, table.find(Properties, Properties["Player"] ) )
PlaySoundRemote:FireClient(Player, SoundId, Location, Properties )
end
end
end
Maybe I’m missing something obvious, but I’m really just at a loss at this point.
If you have any ideas I haven’t tried, please let me know, but for now I’m gonna keep hammering away at it and seeing if I can fix it. Thank you DevForum.