Jump to content

[HELP] Start the sound at a certain vehicle speed.


Recommended Posts

Hello!

I apologize for my bad English.

I'm trying to write a script that will do that if I drive more than 100km/h in the vehicle (vehicle model - 560 (Sultan)), it will start playing speed chime (as in the old Toyota Corolla - click here), but if I drive less than 100km/h, this sound will not play.

Unfortunately, I have no idea how to do something like that.

Could someone please help me? If yes, I will be very grateful!

Link to comment
Posted (edited)
24 minutes ago, Tekken said:


if getElementSpeed(veh, 1) >= 100 then

playSound(...);

end

you can get the useful function 'getElementSpeed' from here: https://wiki.multitheftauto.com/wiki/GetElementSpeed

First of all, I would like to thank you very much for your help.

Well.. can script look like this?

function speedchime()
if getElementSpeed(vehicle, 1) >= 100 then

playSound("speedchime.mp3");

end
addEventHandler("onClientResourceStart", getResourceRootElement(), speedchime)

And meta look like this?

<meta>

<script src="client.lua" type="client"/>
<file src="speedchime.mp3" />

</meta>

 

Edited by DatPsychopath
Change the appearance of the script insertion and meta.
Link to comment

 

hello you can use timer I edited Tekken's code, you can do it like this

function getElementSpeed(theElement, unit)
    -- Check arguments for errors
    assert(isElement(theElement), "Bad argument 1 @ getElementSpeed (element expected, got " .. type(theElement) .. ")")
    local elementType = getElementType(theElement)
    assert(elementType == "player" or elementType == "ped" or elementType == "object" or elementType == "vehicle" or elementType == "projectile", "Invalid element type @ getElementSpeed (player/ped/object/vehicle/projectile expected, got " .. elementType .. ")")
    assert((unit == nil or type(unit) == "string" or type(unit) == "number") and (unit == nil or (tonumber(unit) and (tonumber(unit) == 0 or tonumber(unit) == 1 or tonumber(unit) == 2)) or unit == "m/s" or unit == "km/h" or unit == "mph"), "Bad argument 2 @ getElementSpeed (invalid speed unit)")
    -- Default to m/s if no unit specified and 'ignore' argument type if the string contains a number
    unit = unit == nil and 0 or ((not tonumber(unit)) and unit or tonumber(unit))
    -- Setup our multiplier to convert the velocity to the specified unit
    local mult = (unit == 0 or unit == "m/s") and 50 or ((unit == 1 or unit == "km/h") and 180 or 111.84681456)
    -- Return the speed by calculating the length of the velocity vector, after converting the velocity to the specified unit
    return (Vector3(getElementVelocity(theElement)) * mult).length
end

local soundChime = playSound("speedchime.mp3", true, false)
setSoundPaused(soundChime, true)

function speedchime()
    local theVehicle = getPedOccupiedVehicle(localPlayer)
    if(theVehicle and getElementModel(theVehicle) == 560) then -- is player have vehice and id is 560 ?
       if(getElementSpeed(theVehicle, 1) >= 100) then -- is speed equal or greater then 100 ?
          if(isSoundPaused(soundChime) == true) then -- is sound Paused ?
          setSoundPaused(soundChime, false) -- play sound
          end 
        else
          if(isSoundPaused(soundChime) == false) then -- is sound not paused
          setSoundPaused(soundChime, true) -- pause sound
          end
       end
    end
end
local soundStatusUpdater = setTimer(speedchime, 200, 0)

 

Edited by Burak5312
  • Like 1
Link to comment
17 minutes ago, Burak5312 said:

 

hello you can use timer I edited Tekken's code, you can do it like this


function getElementSpeed(theElement, unit)
    -- Check arguments for errors
    assert(isElement(theElement), "Bad argument 1 @ getElementSpeed (element expected, got " .. type(theElement) .. ")")
    local elementType = getElementType(theElement)
    assert(elementType == "player" or elementType == "ped" or elementType == "object" or elementType == "vehicle" or elementType == "projectile", "Invalid element type @ getElementSpeed (player/ped/object/vehicle/projectile expected, got " .. elementType .. ")")
    assert((unit == nil or type(unit) == "string" or type(unit) == "number") and (unit == nil or (tonumber(unit) and (tonumber(unit) == 0 or tonumber(unit) == 1 or tonumber(unit) == 2)) or unit == "m/s" or unit == "km/h" or unit == "mph"), "Bad argument 2 @ getElementSpeed (invalid speed unit)")
    -- Default to m/s if no unit specified and 'ignore' argument type if the string contains a number
    unit = unit == nil and 0 or ((not tonumber(unit)) and unit or tonumber(unit))
    -- Setup our multiplier to convert the velocity to the specified unit
    local mult = (unit == 0 or unit == "m/s") and 50 or ((unit == 1 or unit == "km/h") and 180 or 111.84681456)
    -- Return the speed by calculating the length of the velocity vector, after converting the velocity to the specified unit
    return (Vector3(getElementVelocity(theElement)) * mult).length
end

local soundChime = playSound("speedchime.mp3", true, false)
setSoundPaused(soundChime, true)

function speedchime()
    local theVehicle = getPedOccupiedVehicle(localPlayer)
    if(theVehicle and getElementModel(theVehicle) == 560) then -- is player have vehice and id is 560 ?
       if(getElementSpeed(theVehicle, 1) >= 100) then -- is speed equal or greater then 100 ?
          if(isSoundPaused(soundChime) == true) then -- is sound Paused ?
          setSoundPaused(soundChime, false) -- play sound
          end 
        else
          if(isSoundPaused(soundChime) == false) then -- is sound not paused
          setSoundPaused(soundChime, true) -- pause sound
          end
       end
    end
end
local soundStatusUpdater = setTimer(speedchime, 200, 0)

 

Thank you very much! It works exactly as I needed it.

Problem solved, so the post can be locked.

Link to comment

 

By the way, if the player suddenly jumps out of the car while driving at 100 kilometers speed, the sound may still work, you can add the following code line for this

if(isPedInVehicle(localPlayer) == false) then setSoundPaused(soundChime, true) end

 

Edited by Burak5312
  • Thanks 1
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...