#RooTs Posted May 24, 2015 Share Posted May 24, 2015 (edited) Hello guys, I'm trying to add a sound in my event. only that the song, not stop repeating when the event is active if ( enablelight ) and ( getVehicleOverrideLights ( theVehicle ) ~= 2 ) then dxDrawImage(x*960+x2, y*470+y2, 306, 232, "files/2/light.png", 0,0,0, tocolor(255, 255, 255 , 255)) playSound("files/lightswitch.mp3") else dxDrawImage(x*960+x2, y*470+y2, 306, 232, "files/2/light2.png", 0,0,0, tocolor(255, 255, 255 , 255)) end any idea? Edited May 24, 2015 by Guest Link to comment
xXMADEXx Posted May 24, 2015 Share Posted May 24, 2015 From the looks of, you're using playSound in an onClientRender event... Therefore, anytime onClientRender is triggered, it's going to play the sound again. Link to comment
SpoC^ Posted May 24, 2015 Share Posted May 24, 2015 @xXMADEXx , @Roots only just want the sound to be enabled once, after entering the vehicle Link to comment
Bilal135 Posted May 24, 2015 Share Posted May 24, 2015 Lol, just put 'false' at the end of playSound. playSound("files/lightswitch.mp3", false) Link to comment
#RooTs Posted May 24, 2015 Author Share Posted May 24, 2015 Lol, just put 'false' at the end of playSound. playSound("files/lightswitch.mp3", false) I've used that way Link to comment
Mr.unpredictable. Posted May 24, 2015 Share Posted May 24, 2015 Lol, just put 'false' at the end of playSound. playSound("files/lightswitch.mp3", false) I've used that way Post your full code. Link to comment
#RooTs Posted May 24, 2015 Author Share Posted May 24, 2015 function speed() if ( enablelight ) and ( getVehicleOverrideLights ( theVehicle ) ~= 2 ) then dxDrawImage(x*960+x2, y*470+y2, 306, 232, "files/2/light.png", 0,0,0, tocolor(255, 255, 255 , 255)) playSound("files/lightswitch.mp3") else dxDrawImage(x*960+x2, y*470+y2, 306, 232, "files/2/light2.png", 0,0,0, tocolor(255, 255, 255 , 255)) end end addEventHandler ( "onClientRender", root, speed ) there is no need for me to post everything Link to comment
WhoAmI Posted May 24, 2015 Share Posted May 24, 2015 local playing = false function speed() if ( enablelight ) and ( getVehicleOverrideLights ( theVehicle ) ~= 2 ) then dxDrawImage(x*960+x2, y*470+y2, 306, 232, "files/2/light.png", 0,0,0, tocolor(255, 255, 255 , 255)) if ( not playing ) then playSound("files/lightswitch.mp3") playing = not playing end else dxDrawImage(x*960+x2, y*470+y2, 306, 232, "files/2/light2.png", 0,0,0, tocolor(255, 255, 255 , 255)) end end addEventHandler ( "onClientRender", root, speed ) Link to comment
#RooTs Posted May 24, 2015 Author Share Posted May 24, 2015 @WhoAmI, only worked once. when I use the event for the second time, it does not work Link to comment
WhoAmI Posted May 24, 2015 Share Posted May 24, 2015 When you are using event again you have to set playing = false. Link to comment
#RooTs Posted May 24, 2015 Author Share Posted May 24, 2015 When you are using event again you have to set playing = false. it is already set to false in line 1 local playing = false -- set to false function speed() if ( enablelight ) and ( getVehicleOverrideLights ( theVehicle ) ~= 2 ) then dxDrawImage(x*960+x2, y*470+y2, 306, 232, "files/2/light.png", 0,0,0, tocolor(255, 255, 255 , 255)) if ( not playing ) then playSound("files/lightswitch.mp3") playing = not playing end else dxDrawImage(x*960+x2, y*470+y2, 306, 232, "files/2/light2.png", 0,0,0, tocolor(255, 255, 255 , 255)) end end addEventHandler ( "onClientRender", root, speed ) Link to comment
#RooTs Posted May 24, 2015 Author Share Posted May 24, 2015 hello personnel. help me, please. does anyone know? oh my god Link to comment
xXMADEXx Posted May 24, 2015 Share Posted May 24, 2015 When you are using event again you have to set playing = false. it is already set to false in line 1 local playing = false -- set to false function speed() if ( enablelight ) and ( getVehicleOverrideLights ( theVehicle ) ~= 2 ) then dxDrawImage(x*960+x2, y*470+y2, 306, 232, "files/2/light.png", 0,0,0, tocolor(255, 255, 255 , 255)) if ( not playing ) then playSound("files/lightswitch.mp3") playing = not playing end else dxDrawImage(x*960+x2, y*470+y2, 306, 232, "files/2/light2.png", 0,0,0, tocolor(255, 255, 255 , 255)) end end addEventHandler ( "onClientRender", root, speed ) Well obviously. After the sound is played, it sets playing to true, and it never gets updated again. Somewhere, you need to set playing to false again. Link to comment
#RooTs Posted May 24, 2015 Author Share Posted May 24, 2015 line 1 local playing = true -- added and not worked Link to comment
xXMADEXx Posted May 24, 2015 Share Posted May 24, 2015 line 1 local playing = true -- added and not worked Do you know how Lua is executed? Anything that isn't in a function, event, or timer is executed ONLY ONCE when the script is first ran. Therefore, you need to either set a timer or create another event that changes the playing variable from true to false. Link to comment
#RooTs Posted May 24, 2015 Author Share Posted May 24, 2015 All my script local sx,sy = guiGetScreenSize() local px,py = 1280,720 local x,y = (sx/px), (sy/py) local x2, y2, x3, y3 = 0, 0, 0, 0 local playing = false -- set to false function speed() local theVehicle = getPedOccupiedVehicle ( getLocalPlayer() ) if ( theVehicle ) then if ( getControlState ( "handbrake" ) ) then dxDrawImage(x*960+x2, y*470+y2, 306, 232, "files/2/hb2.png", 0,0,0, tocolor(255, 255, 255 , 255)) if ( not playing ) then playSound("files/handbrake.wav") playing = not playing end else dxDrawImage(x*960+x2, y*470+y2, 306, 232, "files/2/hb.png", 0,0,0, tocolor(255, 255, 255 , 255)) end end end addEventHandler ( "onClientRender", root, speed ) Link to comment
xXMADEXx Posted May 24, 2015 Share Posted May 24, 2015 All my script local sx,sy = guiGetScreenSize() local px,py = 1280,720 local x,y = (sx/px), (sy/py) local x2, y2, x3, y3 = 0, 0, 0, 0 local playing = false -- set to false function speed() local theVehicle = getPedOccupiedVehicle ( getLocalPlayer() ) if ( theVehicle ) then if ( getControlState ( "handbrake" ) ) then dxDrawImage(x*960+x2, y*470+y2, 306, 232, "files/2/hb2.png", 0,0,0, tocolor(255, 255, 255 , 255)) if ( not playing ) then playSound("files/handbrake.wav") playing = not playing end else dxDrawImage(x*960+x2, y*470+y2, 306, 232, "files/2/hb.png", 0,0,0, tocolor(255, 255, 255 , 255)) end end end addEventHandler ( "onClientRender", root, speed ) Nobody asked for you script, all you had to do is what I said. Try using this: local sx,sy = guiGetScreenSize() local px,py = 1280,720 local x,y = (sx/px), (sy/py) local x2, y2, x3, y3 = 0, 0, 0, 0 local loopTimer = false; function speed() local theVehicle = getPedOccupiedVehicle ( getLocalPlayer() ) if ( theVehicle ) then if ( getControlState ( "handbrake" ) ) then dxDrawImage(x*960+x2, y*470+y2, 306, 232, "files/2/hb2.png", 0,0,0, tocolor(255, 255, 255 , 255)) if ( not isTimer ( loopTimer ) ) then playSound("files/handbrake.wav") loopTimer = setTimer ( function() loopTimer = nil end, 5000, 1 ); end else dxDrawImage(x*960+x2, y*470+y2, 306, 232, "files/2/hb.png", 0,0,0, tocolor(255, 255, 255 , 255)) end end end addEventHandler ( "onClientRender", root, speed ) Link to comment
#RooTs Posted May 24, 2015 Author Share Posted May 24, 2015 fail. in function if ( enablelock ) and ( isVehicleLocked( theVehicle ) ) or ( getElementData( theVehicle, "cl_vehiclelocked" ) ) then the song it is repeating endlessly, after a time Link to comment
Dealman Posted May 25, 2015 Share Posted May 25, 2015 It's looping endlessly because you're running it through onClientRender, you'll be running it every 15-30ms. You can prevent this by using a variable and if statements as shown earlier in this thread - or you can use a separate function that handles the sound. Which triggers via onClientKey or whatever. Link to comment
#RooTs Posted May 25, 2015 Author Share Posted May 25, 2015 @Dealman, I do not have as much knowledge as well. any hint or example is welcome Link to comment
Dealman Posted May 25, 2015 Share Posted May 25, 2015 You could try something like this. I've been away from MTA a while studying C++, so something might not be working as I didn't test it. But it should local hb_BoundKeys = {} local hb_SoundState = false local hb_TheSound = nil function getHandbrakeKeysOnStart_Handler() hb_BoundKeys = getBoundKeys("handbrake") -- Get all the keys they have bound to the Handbrake. end addEventHandler("onClientResourceStart", resourceRoot, getHandbrakeKeysOnStart_Handler) function checkKeyPresses_Handler(theButton, theState) if(isPedInVehicle(localPlayer) == true) then -- Make sure they player's in a vehicle, should minimize the times it's run a bit. You can also add a check to make sure he's in a driver seat. for key, value in ipairs(hb_BoundKeys) do -- Run through the stored keybinds, and check if they match. if(theButton == value) then -- If they match, trigger the function that plays the sound. (I left out theState so it triggers both on press and release.) handbrakeSound_Handler() end end end end addEventHandler("onClientKey", root, checkKeyPresses_Handler) function handbrakeSound_Handler() if(getControlState("handbrake") == true) then -- Another check to make sure the control is active. if(hb_SoundState == false) then -- Only run if hb_SoundState is false, to prevent it from being played twice. hb_SoundState = true -- Update the variable as we're about to play the sound, it will still trigger the rest of the code before the code block ends. hb_TheSound = playSound("files/handbrake.wav", false) -- Play the sound and store it as a variable, this lets you manipulate the sound later on if you want to(Change volume, pitch etc). end else -- If the handbrake control is not true, reset the rest. if(hb_SoundState ~= false) then hb_SoundState = false if(hb_TheSound) then stopSound(hb_TheSound) end end end end Edit: You can also use this code to update what image should be drawn, something like this; local hb_BoundKeys = {} local hb_SoundState = false local hb_TheSound = nil local hb_Image = "files/2/hb.png" function getHandbrakeKeysOnStart_Handler() hb_BoundKeys = getBoundKeys("handbrake") -- Get all the keys they have bound to the Handbrake. end addEventHandler("onClientResourceStart", resourceRoot, getHandbrakeKeysOnStart_Handler) function checkKeyPresses_Handler(theButton, theState) if(isPedInVehicle(localPlayer) == true) then -- Make sure they player's in a vehicle, should minimize the times it's run a bit. You can also add a check to make sure he's in a driver seat. for key, value in ipairs(hb_BoundKeys) do -- Run through the stored keybinds, and check if they match. if(theButton == value) then -- If they match, trigger the function that plays the sound. (I left out theState so it triggers both on press and release.) handbrakeSound_Handler() end end end end addEventHandler("onClientKey", root, checkKeyPresses_Handler) function handbrakeSound_Handler() if(getControlState("handbrake") == true) then -- Another check to make sure the control is active. if(hb_SoundState == false) then -- Only run if hb_SoundState is false, to prevent it from being played twice. hb_SoundState = true -- Update the variable as we're about to play the sound, it will still trigger the rest of the code before the code block ends. hb_TheSound = playSound("files/handbrake.wav", false) -- Play the sound and store it as a variable, this lets you manipulate the sound later on if you want to(Change volume, pitch etc). hb_Image = "files/2/hb2.png" end else -- If the handbrake control is not true, reset the rest. if(hb_SoundState ~= false) then hb_SoundState = false hb_Image = "files/2/hb.png" if(hb_TheSound) then stopSound(hb_TheSound) end end end end Link to comment
#RooTs Posted May 25, 2015 Author Share Posted May 25, 2015 sorry, more I did not understand local hb_Image = "files/2/hb.png" where's dxDrawImage? Link to comment
ALw7sH Posted May 25, 2015 Share Posted May 25, 2015 Maybe there's better ways because i haven't got what do you exactly mean local sx,sy = guiGetScreenSize() local px,py = 1280,720 local x,y = (sx/px), (sy/py) local handbrake = false local x2, y2, x3, y3 = 0, 0, 0, 0 local playing = false -- set to false function speed() local theVehicle = getPedOccupiedVehicle ( getLocalPlayer() ) if ( theVehicle ) then if ( getControlState ( "handbrake" ) ) then dxDrawImage(x*960+x2, y*470+y2, 306, 232, "files/2/hb2.png", 0,0,0, tocolor(255, 255, 255 , 255)) if ( not isElement(handbrake) ) then handbrake = playSound("files/handbrake.wav") end else dxDrawImage(x*960+x2, y*470+y2, 306, 232, "files/2/hb.png", 0,0,0, tocolor(255, 255, 255 , 255)) end end end addEventHandler ( "onClientRender", root, speed ) addEventHandler("onClientSoundStopped",root, function() if source == handbrake then handbrake = false end end ) Link to comment
#RooTs Posted May 25, 2015 Author Share Posted May 25, 2015 @ALw7sH, I'll try. then I tell you if it worked 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