Jump to content

[H.E.L.P]playSound repeating endlessly


#RooTs

Recommended Posts

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 by Guest
Link to comment
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 :wink:

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

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
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

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 :P

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

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

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...