Jump to content

need help cutom sound of vehicle


Red-Pitbull

Recommended Posts

hi all i am new in lua i make a custom sound vehicle i dont khow this script is right but it not working please tell where is problem

--client--

speedx, speedy, speedz = getElementVelocity ( getVehicleID, 411() ) 
function onResourceStart() 
    local sound = playSound3D("sounds/song.mp3", true)  
      setSoundProperties(sound, 48000.0, 128.00, 440.0, false) 
     setSoundSpeed ( sound, 1.2 ) 
end 
  
addEventHandler("onClientResourceStart", getResourceRootElement(getThisResource()), onResourceStart) 

Link to comment
  • Moderators

Ok so, first I assume that the sound you are using is a sample that loop nicely.

Let's then wait and think before going in the code.

The sound must start when the engine is on and only with vehicles with the modelid 411(infernus).

Then we want to play the sound in 3D and playing it for all Infernus on the server.

We need to update the speed of the sound according the speed of the vehicle.

We also need to update the position of the 3D sound to follow the vehicle.

If the engine is off, then we have to pause the sound and resume it if the engine is on again.

Here is a code I just made. With a lot of comments to let you understand what is going on (it's a client script):

function updateEngineSound() 
    -- We get a table of all vehicles on the server: 
    local allVehicles = getElementsByType("vehicle") 
    -- we then do a loop that will iterate over that table: 
    for index, veh in ipairs (allVehicles) do --It's like saying: "For each vehicle, do this" 
        -- we get the model of the vehicle 
        local model = getElementModel(veh) 
        -- if the model is equal to 411 then do this 
        if model == 411 then 
            -- if the engine is on 
            if getVehicleEngineState(veh) then 
                -- we get the position of that vehicle 
                local x, y, z = getElementPosition(veh) 
  
                -- we then try to get the sound we maybe created and stored as an element data 
                -- for that vehicle. If we didn't yet, then it will return false. 
                local sound = getElementData(veh, "engineSound") 
  
                -- if the inverse of sound is true (same as: if sound is equal to false) 
                -- Note that "not false" will give true 
                if not sound then 
                    -- we create the sound at the veh position and loop it 
                    sound = playSound3D("sounds/song.mp3", x, y, z, true) 
                    -- we store that sound in an element data I decided to call "engineSound" 
                    setElementData(veh, "engineSound", sound) 
                end 
  
                -- if the sound is paused (because the engine was off) then 
                if isSoundPaused(veh) then 
                    -- we "unpause" the sound 
                    setSoundPaused(sound, false) 
                end 
  
                -- we then need to get the speed 
                local velocityX, velocityY, velocityZ = getElementVelocity(veh) 
                -- calculate the global speed in mph from the 3 velocities: 
                -- use pythagorean theorem to get actual velocity 
                -- raising something to the exponent of 0.5 is the same thing as taking a square root. 
                local actualspeed = (velocityX^2 + velocityY^2 + velocityZ^2)^(0.5)  
                -- multiply by 50 to obtain the speed in meters per second and 
                -- then by 111.847 to obtain the speed in meters per hour 
                local mph = actualspeed * 50 * 111.847 
  
                -- we try to calculate the sound speed according to the mph 
                -- I guess 80mph is the speed that the original sound sounds like and 
                -- I also guess 0.25 is the min speed that the sound can be at to still sounds like an engine. 
                -- I added 80/minSoundSpeed to 80 to compensate the minSoundSpeed we added 
                local minSoundSpeed = 0.25 
                local soundSpeed = mph/(80+80/minSoundSpeed) + minSoundSpeed 
                -- and then we apply it to the sound 
                setSoundSpeed (sound, soundSpeed) 
  
                -- and finally update the sound position to follow the vehicle 
                setElementPosition(sound, x, y, z) 
  
            else -- otherwise (so if the engine is off) 
                -- we pause the sound 
                setSoundPaused(sound, true) 
            end -- close the "if engine was on" 
        end -- close the "if model was 411" 
    end -- end of the loop 
end 
setTimer(updateEngineSound, 50, 0) --repeat that function every 50 ms 

Hope it helps.

Regards,

Citizen

Link to comment
  
function updateEngineSound() 
    -- We get a table of all vehicles on the server: 
    local allVehicles = getElementsByType("vehicle") 
    -- we then do a loop that will iterate over that table: 
    for index, veh in ipairs (allVehicles) do --It's like saying: "For each vehicle, do this" 
        -- we get the model of the vehicle 
        local model = getElementModel(veh) 
        -- if the model is equal to 411 then do this 
        if model == 411 then 
            -- if the engine is on 
            if getVehicleEngineState(veh) then 
                -- we get the position of that vehicle 
                local x, y, z = getElementPosition(veh) 
  
                -- we then try to get the sound we maybe created and stored as an element data 
                -- for that vehicle. If we didn't yet, then it will return false. 
                local sound = getElementData(veh, "engineSound") 
  
                -- if the inverse of sound is true (same as: if sound is equal to false) 
                -- Note that "not false" will give true 
                if not sound then 
                    -- we create the sound at the veh position and loop it 
                    sound = playSound3D("sounds/song.mp3", x, y, z, true) 
                    -- we store that sound in an element data I decided to call "engineSound" 
                    setElementData(veh, "engineSound", sound) 
                end 
  
                -- if the sound is paused (because the engine was off) then 
                if isSoundPaused(sound) then 
                    -- we "unpause" the sound 
                    setSoundPaused(sound, false) 
                end 
  
                -- we then need to get the speed 
                local velocityX, velocityY, velocityZ = getElementVelocity(veh) 
                -- calculate the global speed in mph from the 3 velocities: 
                -- use pythagorean theorem to get actual velocity 
                -- raising something to the exponent of 0.5 is the same thing as taking a square root. 
                local actualspeed = (velocityX^2 + velocityY^2 + velocityZ^2)^(0.5) 
                -- multiply by 50 to obtain the speed in meters per second and 
                -- then by 111.847 to obtain the speed in meters per hour 
                local mph = actualspeed * 50 * 111.847 
  
                -- we try to calculate the sound speed according to the mph 
                -- I guess 80mph is the speed that the original sound sounds like and 
                -- I also guess 0.25 is the min speed that the sound can be at to still sounds like an engine. 
                -- I added 80/minSoundSpeed to 80 to compensate the minSoundSpeed we added 
                local minSoundSpeed = 0.25 
                local soundSpeed = mph/(80+80/minSoundSpeed) + minSoundSpeed 
                -- and then we apply it to the sound 
                setSoundSpeed (sound, soundSpeed) 
  
                -- and finally update the sound position to follow the vehicle 
                setElementPosition(sound, x, y, z) 
  
            else -- otherwise (so if the engine is off) 
                -- we pause the sound 
                setSoundPaused(sound, true) 
            end -- close the "if engine was on" 
        end -- close the "if model was 411" 
    end -- end of the loop 
end 
setTimer(updateEngineSound, 50, 0) --repeat that function every 50 ms 
  

  • Thanks 1
Link to comment
  • Moderators
again error, 58 lines bad 'sound/player' pointer @ 'SetSoundPaused'(1)

Please post again the full code now you modified it and quote the line 58 if you are removing useless code from the file when copying-pasting here.

Also, be sure you had restarted the resource before testing the code. Since it's a new day, I guess you restarted it.

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