Sound delay - How to fix it?

As you can see, when I shoot it, theres a noticable delay when playing the shooting sound. And after that it doesnt delay anymore until you wait long enough.

Heres a chunk of my code that makes it play the sound.

                local ShootInfo = require(WeaponSettings).ShootSound
				if ShootInfo.Id then
					local NewSound = Instance.new("Sound")
					NewSound.SoundId = ShootInfo.Id
					NewSound.Volume = ShootInfo.Volume or 0.5
					NewSound.Parent = CurrentAttachment
					NewSound:Play()
					Remotes.MakeServerSound:FireServer(CurrentAttachment, "Shoot", WeaponSettings)
					coroutine.wrap(function()
						NewSound.Ended:Wait()
						NewSound:Destroy()
					end)()
				end

Any help is appreciated!

Maybe add a wait before the sound plays / before the bullet is shot?

I think it’s because you are not “preloading” the audio, if you scroll down this page to the “Playing Music” section you’ll see the code example for it.

https://developer.roblox.com/en-us/articles/sounds-and-music

Unfortunately preloading didn’t do the trick.

                local ShootInfo = require(WeaponSettings).ShootSound
				if ShootInfo.Id then
					local NewSound = Instance.new("Sound")
					NewSound.SoundId = ShootInfo.Id
					NewSound.Volume = ShootInfo.Volume or 0.5
					NewSound.Parent = CurrentAttachment
					
					coroutine.wrap(function()
						local nsev
						nsev = NewSound.Ended:Connect(function()
							NewSound:Destroy()
							nsev:Disconnect()
						end)
						
						ContentProvider:PreloadAsync({NewSound})
					end)()
					
					NewSound:Play()
					Remotes.MakeServerSound:FireServer(CurrentAttachment, "Shoot", WeaponSettings)
				end

@StanceFun adding a wait made the delay even more noticable

                local ShootInfo = require(WeaponSettings).ShootSound
				if ShootInfo.Id then
					local NewSound = Instance.new("Sound")
					NewSound.SoundId = ShootInfo.Id
					NewSound.Volume = ShootInfo.Volume or 0.5
					NewSound.Parent = CurrentAttachment
					
					wait()
					
					NewSound:Play()
					Remotes.MakeServerSound:FireServer(CurrentAttachment, "Shoot", WeaponSettings)
					
					coroutine.wrap(function()
						local nsev
						nsev = NewSound.Ended:Connect(function()
							NewSound:Destroy()
							nsev:Disconnect()
						end)
					end)()
				end

It seems the delay is gone when you pre-emptively create a sound in studio and playing the sound by cloning it.

local Sound = ReplicatedStorage.PremadeSound

local function PlaySound(Source)
    local new = Sound:Clone()
    new.Parent = Source
    new:Play()

    new.Ended:Connect(function()
        new:Destroy()
    end)
end
3 Likes

Glad you got a solution, BTW your preload code example didn’t look quite right but it doesn;t matter if you fixed it :slight_smile: