Jump to content

[HELP] Parking Sensor


Mawmad

Recommended Posts

Hello

I'm Working On a Parking Sensor For MTA:SA 

Also I'm New To MTA And Scripting 

My problem is that when I start the resource and move the car in reverse gear, there is no sound

Parkings.lua

-- parking sensor for mta:sa
function isVehicleReversing(theVehicle)
    local theVehicle = getPedOccupiedVehicle ( thePlayer )
    local getMatrix = getElementMatrix (theVehicle)
    local getVelocity = Vector3 (getElementVelocity(theVehicle))
    local getVectorDirection = (getVelocity.x * getMatrix[2][1]) + (getVelocity.y * getMatrix[2][2]) + (getVelocity.z * getMatrix[2][3])
    if (getVectorDirection < 0) then
        return true
function getDistanceBetweenElements(arg1, arg2)
	local car = Vector3(getPedOccupiedVehicle( arg1 ))
	local nearObject = Vector3(getNearestElement( arg2 ))
	local distance = getDistanceBetweenPoints3D( car,nearObject )
	if vehicle then 
	local sound = playSound("beep.mp3")
	setSoundVolume(sound, 0.5) 
	setSoundProperties(sound, 48000.0, 128.00, distance, false)
	return distance
addEventHandler("onClientResourceStart", resourceRoot, isVehicleReversing, getDistanceBetweenElements) -- 

meta.xml

<meta>
    <script src="parkings.lua" type="client" />
    <file src="beep.mp3" type="client" />
</meta>

What is wrong with my script?

  • Like 1
Link to comment
On 23/09/2023 at 02:17, Mawmad said:

Hello

I'm Working On a Parking Sensor For MTA:SA 

Also I'm New To MTA And Scripting 

My problem is that when I start the resource and move the car in reverse gear, there is no sound

Parkings.lua

-- parking sensor for mta:sa
function isVehicleReversing(theVehicle)
    local theVehicle = getPedOccupiedVehicle ( thePlayer )
    local getMatrix = getElementMatrix (theVehicle)
    local getVelocity = Vector3 (getElementVelocity(theVehicle))
    local getVectorDirection = (getVelocity.x * getMatrix[2][1]) + (getVelocity.y * getMatrix[2][2]) + (getVelocity.z * getMatrix[2][3])
    if (getVectorDirection < 0) then
        return true
function getDistanceBetweenElements(arg1, arg2)
	local car = Vector3(getPedOccupiedVehicle( arg1 ))
	local nearObject = Vector3(getNearestElement( arg2 ))
	local distance = getDistanceBetweenPoints3D( car,nearObject )
	if vehicle then 
	local sound = playSound("beep.mp3")
	setSoundVolume(sound, 0.5) 
	setSoundProperties(sound, 48000.0, 128.00, distance, false)
	return distance
addEventHandler("onClientResourceStart", resourceRoot, isVehicleReversing, getDistanceBetweenElements) -- 

meta.xml

<meta>
    <script src="parkings.lua" type="client" />
    <file src="beep.mp3" type="client" />
</meta>

What is wrong with my script?

There are a lot of problems with your script
Try this:
 

local alarm = nil

bindKey("s", "down", function()
    local vehicle = getPedOccupiedVehicle(localPlayer)
	if vehicle then
	   if isVehicleReversing(vehicle) then
	      local nearObject = getNearestElement(vehicle, "object", 15)
		  if nearObject then
		     if alarm == nil then
			    alarm = playSound("beep.mp3", false)
		     end
		  end
	   end
	end
end)



function ForceSoundOff()
   local vehicle = getPedOccupiedVehicle(localPlayer)
   if not isVehicleReversing(vehicle) then
      if alarm ~= nil then
	     stopSound(alarm)
		 alarm = nil
	  end
   end
end
addEventHandler("onClientRender", root, ForceSoundOff)


function getNearestElement(player, type, distance)
	local result = false
	local dist = nil
	if player and isElement(player) then
		local elements = getElementsWithinRange(Vector3(getElementPosition(player)), distance, type, getElementInterior(player), getElementDimension(player))
		for i = 1, #elements do
			local element = elements[i]
			if not dist then
				result = element
				dist = getDistanceBetweenPoints3D(Vector3(getElementPosition(player)), Vector3(getElementPosition(element)))
			else
				local newDist = getDistanceBetweenPoints3D(Vector3(getElementPosition(player)), Vector3(getElementPosition(element)))
				if newDist <= dist then
					result = element
					dist = newDist
				end
			end
		end
	end
	return result
end

function isVehicleReversing(theVehicle)
    local getMatrix = getElementMatrix (theVehicle)
    local getVelocity = Vector3 (getElementVelocity(theVehicle))
    local getVectorDirection = (getVelocity.x * getMatrix[2][1]) + (getVelocity.y * getMatrix[2][2]) + (getVelocity.z * getMatrix[2][3])
    if (getVectorDirection < 0) then
        return true
    end
    return false
end

 

Link to comment
39 minutes ago, Mawmad said:

Wow , Thanks a Lot Mate ! ❤️

 

Sorry and it still doesn't work...

Everything seems fine, I don't know what the problem is

If you don't hear the sound, try to look for one for about 2-3 seconds maximum and in playSound("beep.mp3", false) put playSound("beep.mp3", true) so that the sound repeats. if this don't work idk then

Link to comment
On 23/09/2023 at 02:17, Mawmad said:

Hello

I'm Working On a Parking Sensor For MTA:SA 

Also I'm New To MTA And Scripting 

My problem is that when I start the resource and move the car in reverse gear, there is no sound

Parkings.lua

-- parking sensor for mta:sa
function isVehicleReversing(theVehicle)
    local theVehicle = getPedOccupiedVehicle ( thePlayer )
    local getMatrix = getElementMatrix (theVehicle)
    local getVelocity = Vector3 (getElementVelocity(theVehicle))
    local getVectorDirection = (getVelocity.x * getMatrix[2][1]) + (getVelocity.y * getMatrix[2][2]) + (getVelocity.z * getMatrix[2][3])
    if (getVectorDirection < 0) then
        return true
function getDistanceBetweenElements(arg1, arg2)
	local car = Vector3(getPedOccupiedVehicle( arg1 ))
	local nearObject = Vector3(getNearestElement( arg2 ))
	local distance = getDistanceBetweenPoints3D( car,nearObject )
	if vehicle then 
	local sound = playSound("beep.mp3")
	setSoundVolume(sound, 0.5) 
	setSoundProperties(sound, 48000.0, 128.00, distance, false)
	return distance
addEventHandler("onClientResourceStart", resourceRoot, isVehicleReversing, getDistanceBetweenElements) -- 

meta.xml

<meta>
    <script src="parkings.lua" type="client" />
    <file src="beep.mp3" type="client" />
</meta>

What is wrong with my script?

remove type client from there maybe that could be the problem (from client i mean)

-> u only need this ->

<file src="beep.mp3"/>

 

Edited by FlorinSzasz
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...