Jump to content

Help. Sound Script.


Eddy32HD

Recommended Posts

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 by Eddy32HD
Link to comment
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 by Dimos7
Link to comment
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
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
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

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 by LoPollo
Link to comment
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
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

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 by LoPollo
Link to comment

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 by Savannah
  • Like 1
Link to comment
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

  • Like 1
Link to comment
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 by Savannah
  • Like 2
Link to comment
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:):):) 

  • Like 1
Link to comment

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 by LoPollo
Link to comment

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...