• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

[OTClient Mehaia 3.0.2] Randomizing soundtrack

Siegh

Thronar Developer
Joined
Mar 12, 2011
Messages
1,298
Solutions
1
Reaction score
681
Location
Brazil
Hello! I'm trying to have my client play random songs from a list in a loop but, while I do manage to make it randomize songs from a list and keep playing as a loop, as soon as it chooses one music it will stick with it indefinitely.

How can I have it randomize its pick after a song plays entirely?

This is on client.lua:

LUA:
local musicTracks = {
  "/sounds/startup",
  "/sounds/townMusic1",
  "/sounds/townMusic2",
  --"/sounds/dungeonMusic1",
  --"/sounds/dungeonMusic2",
}

local randomTrack = musicTracks[math.random(1,#musicTracks)]

function startup()
    if musicChannel then
        musicChannel:enqueue(musicTracks[math.random(1,#musicTracks)], 3)
        connect(g_game, {
            onGameStart = function()
                musicChannel:stop(3)
                local currentMusic = musicChannel:enqueue(musicTracks[math.random(1,#musicTracks)], 3)
                if currentMusic then
                    currentMusic.onFinish = function()
                        -- Recursive call to pick and play the next random song
                        musicChannel:stop(3)
                        musicChannel:enqueue(musicTracks[math.random(1,#musicTracks)], 3)
                    end
                end
            end
        })
        connect(g_game, {
            onGameEnd = function()
                musicChannel:stop(3)
                local currentMusic = musicChannel:enqueue(musicTracks[math.random(1,#musicTracks)], 3)
                if currentMusic then
                    currentMusic.onFinish = function()
                        -- Recursive call to pick and play the next random song
                        musicChannel:stop(3)
                        musicChannel:enqueue(musicTracks[math.random(1,#musicTracks)], 3)
                    end
                end
            end
        })
    end
 
Back
Top