Bean666 Posted March 9, 2021 Share Posted March 9, 2021 (edited) Hi i tried to do a fade out effect when stopping music, but I did it poorly, it works fine no errors, but sometimes it gives an error, it says bad sound pointer? especially when we're more than 3 players that have the music played. at the volume variable and the setSoundVolume(bossmusic, volume-0.1) function bossmusic() if isTimer(fadeout) then stopSound(bossmusic) killTimer(fadeout) end if isTimer(removefade) then killTimer(removefade) end bossmusic = playSound("sounds/theme.mp3", true) setSoundVolume(bossmusic, 0.7) end addEvent("playmusic", true) addEventHandler("playmusic", root, bossmusic) function stopmusic() if isElement(bossmusic) then fadeout = setTimer(function() volume = getSoundVolume(bossmusic) setSoundVolume(bossmusic, volume-0.1) end, 1000, 0) removefade = setTimer(function() killTimer(fadeout) stopSound(bossmusic) end, 15000, 1) end end addEvent("stopmusic", true) addEventHandler("stopmusic", root, stopmusic) Edited March 9, 2021 by Bean666 Link to comment
Moderators IIYAMA Posted March 9, 2021 Moderators Share Posted March 9, 2021 (edited) 15 minutes ago, Bean666 said: Hi i tried to do a fade out effect when stopping music, but I did it poorly, it works fine no errors, but sometimes it gives an error, it says bad sound pointer? especially when we're more than 3 players that have the music played. There is a 1 second delay between validation > if bossmusic is an element. You will have to validate again. The 1 second delay is where that 'sometimes' comes in. if isElement(bossmusic) then -- validation fadeout = setTimer(function() -- delay -- missing revalidation volume = getSoundVolume(bossmusic) setSoundVolume(bossmusic, volume-0.1) end, 1000, 0) --[[ ... ]] Edited March 9, 2021 by IIYAMA Link to comment
Bean666 Posted March 9, 2021 Author Share Posted March 9, 2021 1 minute ago, IIYAMA said: There is a 1 second delay between validation > if bossmusic is an element. You will have to validate again. The 1 second delay is where that 'sometimes' comes in. if isElement(bossmusic) then -- validation fadeout = setTimer(function() -- delay -- missing revalidation volume = getSoundVolume(bossmusic) setSoundVolume(bossmusic, volume-0.1) end, 1000, 0) --[[ ... ]] function stopmusic() if isElement(bossmusic) then fadeout = setTimer(function() if isElement(bossmusic) then volume = getSoundVolume(bossmusic) setSoundVolume(bossmusic, volume-0.1) end end, 1000, 0) removefade = setTimer(function() killTimer(fadeout) stopSound(bossmusic) end, 15000, 1) end end addEvent("stopmusic", true) addEventHandler("stopmusic", root, stopmusic) basically like this? i add another revalidation. i got what u meant but is this right? Link to comment
Moderators IIYAMA Posted March 9, 2021 Moderators Share Posted March 9, 2021 31 minutes ago, Bean666 said: but is this right? Yes Maybe here as well, but you probably already figured that one out. 31 minutes ago, Bean666 said: stopSound(bossmusic) Link to comment
SpecT Posted March 9, 2021 Share Posted March 9, 2021 (edited) 3 hours ago, Bean666 said: function stopmusic() if isElement(bossmusic) then fadeout = setTimer(function() if isElement(bossmusic) then volume = getSoundVolume(bossmusic) setSoundVolume(bossmusic, volume-0.1) end end, 1000, 0) removefade = setTimer(function() killTimer(fadeout) stopSound(bossmusic) end, 15000, 1) end end addEvent("stopmusic", true) addEventHandler("stopmusic", root, stopmusic) basically like this? i add another revalidation. i got what u meant but is this right? Isn't it better in the first timer function to check the sound volume and once it gets to 0 => kill the timer (fadeout) and stop the sound. This way you won't have to create another timer which will kill the first timer. The less timers the better. Edited March 9, 2021 by SpecT 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