Jump to content

Cant get script to play 2 different sounds


micheal1230

Recommended Posts

When you press 'n' you get a siren sound, but when you press 'n' again you get a different siren sound but the second sound wont play!

local sounds = { } 
  
-- Bind Keys required 
function bindKeys(res) 
    bindKey("n", "down", toggleSirens) 
     
    for key, value in ipairs(getElementsByType("vehicle")) do 
        if isElementStreamedIn(value) then 
            if getElementData(value, "lspd:siren") then 
                sounds[value] = playSound3D("siren.wav", 0, 0, 0, true) 
                attachElements( sounds[value], value ) 
                setSoundVolume(sounds[value], 0.6) 
                setSoundMaxDistance(sounds[value], 45) 
                setElementDimension(sounds[value], getElementDimension(value)) 
                setElementInterior(sounds[value], getElementInterior(value)) 
            end 
        end 
    end 
end 
addEventHandler("onClientResourceStart", getResourceRootElement(), bindKeys) 
  
function toggleSirens() 
    local theVehicle = getPedOccupiedVehicle(getLocalPlayer()) 
     
    if (theVehicle) then 
        if getElementData(theVehicle, "lspd:siren") then 
            triggerServerEvent("lspd:setSirenState", theVehicle, false) 
        elseif (exports.global:hasItem(theVehicle, 85)) then 
            triggerServerEvent("lspd:setSirenState", theVehicle, true) 
        end 
    end 
end 
addCommandHandler("togglecarsirens", toggleSirens, false) 
  
function streamIn() 
    if getElementType( source ) == "vehicle" and getElementData( source, "lspd:siren" ) and not sounds[ source ] then 
        sounds[source] = playSound3D("siren.wav", 0, 0, 0, true) 
        attachElements( sounds[source], source ) 
        setSoundVolume(sounds[source], 0.6) 
        setSoundMaxDistance(sounds[source], 45) 
        setElementDimension(sounds[source], getElementDimension(source)) 
        setElementInterior(sounds[source], getElementInterior(source)) 
    end 
end 
addEventHandler("onClientElementStreamIn", getRootElement(), streamIn) 
  
function streamOut() 
    if getElementType( source ) == "vehicle" and sounds[source] then 
        destroyElement( sounds[ source ] ) 
        sounds[ source ] = nil 
    end 
end 
addEventHandler("onClientElementStreamOut", getRootElement(), streamOut) 
  
function updateSirens( name ) 
    if name == "lspd:siren" and isElementStreamedIn( source ) and getElementType( source ) == "vehicle" then 
        local attached = getAttachedElements( source ) 
        if attached then 
            for key, value in ipairs( attached ) do 
                if getElementType( value ) == "sound" and value ~= sounds[ source ] then 
                    destroyElement( value ) 
                end 
            end 
        end 
         
        if not getElementData( source, name ) then 
            if sounds[ source ] then 
                destroyElement( sounds[ source ] ) 
                sounds[ source ] = nil 
            end 
        else 
            if not sounds[ source ] then 
                sounds[source] = playSound3D("siren.wav", 0, 0, 0, true) 
                attachElements( sounds[source], source ) 
                setSoundVolume(sounds[source], 0.6) 
                setSoundMaxDistance(sounds[source], 45) 
                setElementDimension(sounds[source], getElementDimension(source)) 
                setElementInterior(sounds[source], getElementInterior(source)) 
            end 
        end 
    end 
end 
addEventHandler("onClientElementDataChange", getRootElement(), updateSirens) 

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