[Question] PlayMusicInLoop

Hello. First of all, I’m using this script: [Release] Play Custom Sounds for Interactions
For those who doesn’t know how that works it simply starts playing sound from .ogg file. The problem is there is no loop option or anything like that, so when file ends the music is gone until another request is send. I wanted to make a function which will let playing this music in loop but recently I was told that TerminateThisThread() doesn’t work in FiveM, so I can’t use it that much because it will create many threads and I already had tons of problems with that because it was crashing my script in random moments because of that.

function PlayMusicInLoop(index, audio, length)
    KeepPlaying[index] = true
    Citizen.CreateThread(function()
        while KeepPlaying[index] do
            TriggerEvent('LIFE_CL:Sound:PlayOnOne', audio, 0.05)
            Citizen.Wait(length)
        end
        TerminateThisThread()
    end)
end

And this is why I’m asking if anyone has a better idea for the structure in this little function. I really appreciate any tips :smiley:

what? threads are ‘terminated’ right when they end, your script doesn’t ‘crash in random moments’ because you’re not trying to use a 100% unrelated function in fivem

No, it’s not because of this unrelated function. It’s because it seems like this thread doesn’t really end when it should. When I use this function and the time where it should it be terminated comes the game gets “freeze” effect and throws an error in the native which is currently working.

Feel free to look at how I achieve a loop here, from line 171:

https://github.com/inferno-collection/Fire-Alarm/blob/master/[inferno-collection]/inferno-fire-alarm/client.lua

1 Like

Wow, it seems that it works perfectly. You are a real life saver man. I really appreciate it :smiley:

1 Like