CSoundPatch cuts out too early and stops sound

I’ve seen a lot of threads where people are having problems with CSoundPatch, but they’re all from 2009 (so I don’t know if a recent update messed with CSoundPatch) and none of them give me a clear answer to my problem anyways. When I try to play a sound it will just cut out after a few seconds, even if the sound I use (like in this example) is at least thirty seconds long (and supposed to loop). What’s going on here? What I’m I doing wrong, or is CSoundPatch just broken?

The closest I came to finding a solution was with this thread, but I’m not sure exactly what he means. I tried making all the sound related variables I was using global (since someone further down said that was what Azui meant), but it still didn’t work.

This is an example of what I’m trying to do:

function cspatchtest( ply )

local outsideSound = Sound( "ambient/ambience/wind_light02_loop.wav" ) 
local outsideSoundStart = CreateSound(  ply,  outsideSound )



concommand.Add( “cspatchtest”, cspatchtest )


Any help would be greatly appreciated.

If I understood it right what he meant:

Your SoundVariable is local so as soon as it is out of the scope it will get deleted, so I guess the sound too. Solution: Make it a global!


I’ll look into this when I get home.


CSPatch = NULL

local function LocalTest(ply)

local s = Sound("ambient/ambience/wind_light02_loop.wav")
local csp = CreateSound(ply, s)

concommand.Add(“CSPTestL”, LocalTest)

local function GlobalTest(ply)

local s = Sound("ambient/ambience/wind_light02_loop.wav") -- All this does is precache and return the string again, no need to make it global.
CSPatch = CreateSound(ply, s)


concommand.Add(“CSPTestG”, GlobalTest)

Local test gave me 1-5 seconds of the sound, then cut off.

Global test however, worked fine. The sound did not stop.

Code was ran on server.

Now that I’ve tried it again, it’s working, maybe I made the sound catch global and not the actual CreateSound. Thank you!


Edit: The reason it was stopping and starting for me was because the place I was putting it was inside a loop.