Eddy32HD Posted December 3, 2016 Share Posted December 3, 2016 (edited) I cant seem to find a solution to have a toggle system for a sound to play. For Example: If i press "b" The sirens should go off, if i press "b" again it should stop. This is what I want to happen. when i press "b" the siren should sound, and when i press "b" again the siren should stop. CODE (Client-Side) --Sirens Section function sound003 (player) local sound = playSound("Sirens/Sound_003.wav") setSoundVolume(sound, 0.3) end addCommandHandler("sound003", sound003) bindKey("num_1", "down", "sound003") function sound012 (player) local sound = playSound("Sirens/Sound_012.wav") setSoundVolume(sound, 0.3) end addCommandHandler("sound012", sound012) bindKey("n", "down", "sound012") every time I press the bindKey it keeps spamming the sound. Edited December 3, 2016 by Eddy32HD Link to comment
Dimos7 Posted December 3, 2016 Share Posted December 3, 2016 (edited) function sound003() local sound = playSound("Sirens/Sound_003.wav") setSoundVolume(sound, 0.3) if sound then stopSound(sound) end end addCommandHandler("sound003", sound003) bindKey("num_1", "down", "sound003") function sound012() local sound = playSound("Sirens/Sound_012.wav") setSoundVolume(sound, 0.3) if sound then stopSound(sound) end end addCommandHandler("sound012", sound012) bindKey("n", "down", "sound012") Second way: function sound003() local sound = playSound("Sirens/Sound_003.wav") setSoundVolume(sound, 0.3) if getSoundVolume(sound)== 0.3 then setSoundVolume(sound, 0.0) end end addCommandHandler("sound003", sound003) bindKey("num_1", "down", "sound003") function sound012() local sound = playSound("Sirens/Sound_012.wav") setSoundVolume(sound, 0.3) if getSoundVolume(sound) == 0.3 then setSoundVolume(sound, 0.0) end end addCommandHandler("sound012", sound012) bindKey("n", "down", "sound012") Edited December 3, 2016 by Dimos7 Link to comment
Eddy32HD Posted December 3, 2016 Author Share Posted December 3, 2016 1 hour ago, Dimos7 said: function sound003() local sound = playSound("Sirens/Sound_003.wav") setSoundVolume(sound, 0.3) if sound then stopSound(sound) end end addCommandHandler("sound003", sound003) bindKey("num_1", "down", "sound003") function sound012() local sound = playSound("Sirens/Sound_012.wav") setSoundVolume(sound, 0.3) if sound then stopSound(sound) end end addCommandHandler("sound012", sound012) bindKey("n", "down", "sound012") Second way: function sound003() local sound = playSound("Sirens/Sound_003.wav") setSoundVolume(sound, 0.3) if getSoundVolume(sound)== 0.3 then setSoundVolume(sound, 0.0) end end addCommandHandler("sound003", sound003) bindKey("num_1", "down", "sound003") function sound012() local sound = playSound("Sirens/Sound_012.wav") setSoundVolume(sound, 0.3) if getSoundVolume(sound) == 0.3 then setSoundVolume(sound, 0.0) end end addCommandHandler("sound012", sound012) bindKey("n", "down", "sound012") Not working, Must be on the volume error, server log hasn't detect an error Link to comment
Walid Posted December 4, 2016 Share Posted December 4, 2016 local play = {} function pSound(key,keyState,s) if play[s] then stopSound(play[s]) else play[s] = playSound("Sirens/"..s..".wav") setSoundVolume(play[s], 0.3) end end bindKey("num_1", "down", pSound,"Sound_003") bindKey("n", "down", pSound,"Sound_012") Link to comment
Eddy32HD Posted December 4, 2016 Author Share Posted December 4, 2016 12 minutes ago, Walid said: local play = {} function pSound(key,keyState,s) if play[s] then stopSound(play[s]) else play[s] = playSound("Sirens/"..s..".wav") setSoundVolume(play[s], 0.3) end end bindKey("num_1", "down", pSound,"Sound_003") bindKey("n", "down", pSound,"Sound_012") This works, but it only plays one time. Link to comment
Walid Posted December 4, 2016 Share Posted December 4, 2016 play[s] = playSound("Sirens/"..s..".wav",true) Link to comment
Eddy32HD Posted December 4, 2016 Author Share Posted December 4, 2016 2 minutes ago, Walid said: play[s] = playSound("Sirens/"..s..".wav",true) That's seems better but it wont activate again. Link to comment
LoPollo Posted December 4, 2016 Share Posted December 4, 2016 (edited) try this UNTESTED local sounds = {} --Sirens Section function sound003 (player) if not sounds["003"] then sounds["003"] = playSound("Sirens/Sound_003.wav") setSoundVolume(sounds["003"], 0.3) else stopSound( sounds["003"] ) destroyElement( sounds["003"] ) sounds["003"] = nil end end addCommandHandler("sound003", sound003) bindKey("num_1", "down", "sound003") function sound012 (player) if not sounds["012"] then sounds["012"] = playSound("Sirens/Sound_012.wav") setSoundVolume(sounds["012"], 0.3) else stopSound( sound["012"] ) destroyElement( sounds["012"] ) sounds["012"] = nil end end addCommandHandler("sound012", sound012) bindKey("n", "down", "sound012") EDIT: also the way @Walid use (one function to control all the sounds identified by a string) is better than this if all the sound files have the same name as the string you use as ID in the script. If you want i can post a function using that logic Edited December 4, 2016 by LoPollo Link to comment
Eddy32HD Posted December 4, 2016 Author Share Posted December 4, 2016 13 minutes ago, LoPollo said: try this UNTESTED local sounds = {} --Sirens Section function sound003 (player) if not sounds["003"] then sounds["003"] = playSound("Sirens/Sound_003.wav") setSoundVolume(sounds["003"], 0.3) else stopSound( sounds["003"] ) destroyElement( sounds["003"] ) sounds["003"] = nil end end addCommandHandler("sound003", sound003) bindKey("num_1", "down", "sound003") function sound012 (player) if not sounds["012"] then sounds["012"] = playSound("Sirens/Sound_012.wav") setSoundVolume(sounds["012"], 0.3) else stopSound( sound["012"] ) destroyElement( sounds["012"] ) sounds["012"] = nil end end addCommandHandler("sound012", sound012) bindKey("n", "down", "sound012") EDIT: also the way @Walid use (one function to control all the sounds identified by a string) is better than this if all the sound files have the same name as the string you use as ID in the script. If you want i can post a function using that logic This works perfectly, but sound012 is not acting like sound003, I tried changing the bindKey for sound012 and didn't work. Link to comment
LoPollo Posted December 4, 2016 Share Posted December 4, 2016 4 minutes ago, Eddy32HD said: but sound012 is not acting like sound003 Explain Link to comment
Eddy32HD Posted December 4, 2016 Author Share Posted December 4, 2016 Just now, LoPollo said: Explain Ok Sound003 is a Police car Air horn which i had to set a loop to the sound. Looped Air horn, Works great I can toggle it on and off. sounds["003"] = playSound("Sirens/Sound_003.wav",true) But Sound012 is a regular wailing siren that can only be toggled on and not off, even though I looped the sound. Link to comment
LoPollo Posted December 4, 2016 Share Posted December 4, 2016 (edited) So both are looped (true as second arg of playSound) but only the 003 can be stopped? EDIT: oh i'm stupid, only 003 is looped and it's ok, but 012 can be started but can't be stopped. So you want to prevent "overlapping" right? Edited December 4, 2016 by LoPollo Link to comment
Savannah Posted December 4, 2016 Share Posted December 4, 2016 (edited) Eddy, what is it you need. Just a simple sound toggle? function sound003(player) if isElement(sound) then stopSound(sound) else sound = playSound("Sirens/Sound_003.wav") setSoundVolume(sound, 0.3) end end addCommandHandler("sound003", sound003) bindKey("num_1", "down", "sound003") Edited December 4, 2016 by Savannah 1 Link to comment
LoPollo Posted December 4, 2016 Share Posted December 4, 2016 (edited) @Savannah ... i would check again your code, when sound is an element, it will be destroyed and then recreated. Also we solved this issue already. If this was to prevent the overlapping sound, then we don't know what @Turbo777 exactly want Edited December 4, 2016 by LoPollo Link to comment
Savannah Posted December 4, 2016 Share Posted December 4, 2016 (edited) @LoPollo Fixed and edited it, my bad. You don't have to use tables if you're just trying to control one sound. You use isElement(sound) to check if it's playing. Edited December 4, 2016 by Savannah Link to comment
Eddy32HD Posted December 4, 2016 Author Share Posted December 4, 2016 9 hours ago, LoPollo said: So both are looped (true as second arg of playSound) but only the 003 can be stopped? EDIT: oh i'm stupid, only 003 is looped and it's ok, but 012 can be started but can't be stopped. So you want to prevent "overlapping" right? I just want the sound to be turn on and off. 49 minutes ago, Savannah said: Eddy, what is it you need. Just a simple sound toggle? function sound003(player) if isElement(sound) then stopSound(sound) else sound = playSound("Sirens/Sound_003.wav") setSoundVolume(sound, 0.3) end end addCommandHandler("sound003", sound003) bindKey("num_1", "down", "sound003") Yes 1 Link to comment
Eddy32HD Posted December 4, 2016 Author Share Posted December 4, 2016 (edited) I made a quick video explaining the situation. (BY THE WAY) SOUND012 IS NOW SOUND011, I changed the file to a different siren effect. sorry if I'm confusing some. Edited December 4, 2016 by Eddy32HD Link to comment
Savannah Posted December 4, 2016 Share Posted December 4, 2016 (edited) function horn(player) if isElement(horn) then stopSound(horn) else horn = playSound("Sirens/Sound_003.wav") setSoundVolume(horn, 0.3) end end addCommandHandler("horn", horn) bindKey("num_1", "down", horn) function siren(player) if isElement(siren) then stopSound(siren) else siren = playSound("Sirens/Sound_012.wav") setSoundVolume(siren, 0.3) end end addCommandHandler("siren", siren) bindKey("num_2", "down", siren) This should work. Fixed the binds for you too. Edited December 4, 2016 by Savannah 2 Link to comment
Eddy32HD Posted December 4, 2016 Author Share Posted December 4, 2016 22 minutes ago, Savannah said: function horn(player) if isElement(horn) then stopSound(horn) else horn = playSound("Sirens/Sound_003.wav") setSoundVolume(horn, 0.3) end end addCommandHandler("horn", horn) bindKey("num_1", "down", horn) function siren(player) if isElement(siren) then stopSound(siren) else siren = playSound("Sirens/Sound_012.wav") setSoundVolume(siren, 0.3) end end addCommandHandler("siren", siren) bindKey("num_2", "down", siren) This should work. Fixed the binds for you too. This works 100% Great job!!! Thank you everyone for helping out @LoPollo @Walid @Dimos7 1 Link to comment
LoPollo Posted December 4, 2016 Share Posted December 4, 2016 (edited) Just so i know The differences with what @Savannah posted and mine were (talking about errors): stopSound also destroy the element, so destroyelement was useless and also should have thrown a warning I did not correct an error with bindKey parameter All here? I ask to make sure i don't make the same errors next time. But other than differences that are not errors (like tables, nilling the value instead of checking the element existence etc) i don't find anything else. Also a little note that could prevent bugs. When destroying an element (so after using stopSound, i learned that also destoys the element*) is a good practice to nil the variable, to prevent bugs due to id recycle**. I say this cause i don't think that stopSound nils the variable, since it don't have access to it Spoiler * stopSound: Stops the sound playback for specified sound element. The sound element is also destroyed. ** destroyElement Note: As element ids are eventually recycled, always make sure you nil variables containing the element after calling this function Edited December 4, 2016 by LoPollo 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