DNL291 Posted October 14, 2017 Share Posted October 14, 2017 I still can't understand why you need a timer for it. On 09/10/2017 at 03:05, kieran said: Hi, I am trying to make a script that will play a random sound from a table and then continue to play sounds, but I have a problem with the timer, all sounds play at once instead of one after the other... Can you explain better, please? Because reading your quote, I assume you want the next song to play after the previous one finished, and the code I wrote works that way. Anyway, If you want to access the timer variable globally, just do as I did with 'playingSound' variable in my code above. Link to comment
kieran Posted October 14, 2017 Author Share Posted October 14, 2017 (edited) 34 minutes ago, DNL291 said: I still can't understand why you need a timer for it. Can you explain better, please? Because reading your quote, I assume you want the next song to play after the previous one finished, and the code I wrote works that way. Anyway, If you want to access the timer variable globally, just do as I did with 'playingSound' variable in my code above. Worked perfect, and yes, I had a problem first with milliseconds, but then I realized I set it to *100 and not *1000 of sounds length But after that I had a problem making my timer global as it was local inside another function, tried your code and works perfect! Thanks Did make one edit though, a useful tip I found out is... Always remove a onClientResourceStart handler if it's just when the player logs on or joins, prevents it restarting Edited October 14, 2017 by kieran Link to comment
DNL291 Posted October 14, 2017 Share Posted October 14, 2017 Glad it worked, you're welcome. 1 Link to comment
ShayF2 Posted October 22, 2017 Share Posted October 22, 2017 (edited) songs = { 'sounds/1.mp3', 'sounds/2.mp3', 'sounds/3.mp3', 'sounds/4.mp3', 'sounds/5.mp3', 'sounds/6.mp3', 'sounds/7.mp3', 'sounds/8.mp3' } local previous function randomIndex() local number = math.random(1,#songs) if not previous == number then previous = number return number end end function nextSong() if isSoundFinished(sound) then playSong() else setTimer(nextSong,2000,1) end end local sound function playSong() local i = randomIndex() sound = playSound(songs[i],true) if isElement(sound) then setSoundVolume(sound,0.8) local length = getSoundLength(sound) if length > 1 then length = length+2000 end setTimer(nextSong,length,1) end end addEventHandler('onClientResourceStart',resourceRoot,playSong) function stopLoginSound() if isElement(sound) then destroyElement(sound) end end addEvent('stoploginsound',true) addEventHandler('stoploginsound',root,stopLoginSound) This is simply proper code formatting, I've got no idea if it works. If it does then it does. It's suppose to lol. Good luck. Just remember getSoundLength returns in milliseconds and a timer also uses milliseconds... 1000 milliseconds = 1 second. Edited October 22, 2017 by ShayF Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now