Jump to content

How to detect vehicle's HP?


Recommended Posts

the event triggers because u dont use the loss parameter to check the amount of health the vehicle lost in onClientVehicleDamage 

 

addEventHandler("onClientVehicleDamage", root,
    function(loss)
        local vehHP = getElementHealth(source)
        if currentHealth - loss <= 0 then
        end
    end)
  

 

  • Like 1
Link to comment
7 hours ago, Hiding said:

I tried with onClientVehicleDamage, but it only triggered when my car was exploded. I need the localPlayer vehicle's health before the explosion. 

you can get the pilot with getVehicleOccupant function and check if it exists

an example that maybe can help you

function VehicleHealth(attacker, weapon, loss)
    local HealthOfVehicle = getElementHealth(source)
    local driver = getVehicleOccupant(source)
    if driver then
        outputChatBox("life before the crash: "..math.floor(HealthOfVehicle))
        outputChatBox("life after the crash: "..math.floor(HealthOfVehicle - loss))
        outputChatBox("damage taken: "..math.floor(loss))
    end
end
addEventHandler("onClientVehicleDamage", root, VehicleHealth)
Edited by SciptNovato
  • Like 1
Link to comment

Thank you guys so much for trying to help me, but I guess you don't get it :D maybe I have to check with onClientRender when the HP reaches 0, because this onClientVehicleDamage is only triggering if damage happens, but for example when the vehicle is upside down and starts burning, it doesn't actually suffer any damage until it explodes and its life reaches zero. So what I would need is that as soon as the vehicle reaches zero hp output to the chat that "vehicle hp is 0" or something. But if I check it with onClientRender then it will flood the chat until my hp is 0, how can I manage to write once in the chat if the hp is reaching 0?

Edited by Hiding
Link to comment

Hi

 

I noticed that :

- The vehicle starts to be on fire below 250 / 1000

- When it's flipped it takes 1100 of damage during the explosion

I don't see any event about detecting the vehicle on fire but you can still detect it like this :
    

 

addEventHandler("onClientVehicleEnter", root, function() 
	local vehicle = source
	setTimer(function()
		if getElementHealth(vehicle) < 250 then
			print("onFire")
			killTimer(sourceTimer)
		end
	end, 500, 0)
	
end)
	

 

 

Edited by Mkl
  • Like 1
Link to comment
1 hour ago, Hiding said:

Thank you guys so much for trying to help me, but I guess you don't get it &amp;amp;lt;img alt=":D" data-emoticon="" height="20" src="https://forum.multitheftauto.com/uploads/emoticons/biggrin.png" srcset="https://forum.multitheftauto.com/uploads/emoticons/[email protected] 2x" title=":D" width="20" /&amp;amp;gt;  maybe I have to check with onClientRender when the HP reaches 0, because this onClientVehicleDamage is only triggering if damage happens, but for example when the vehicle is upside down and starts burning, it doesn't actually suffer any damage until it explodes and its life reaches zero. So what I would need is that as soon as the vehicle reaches zero hp output to the chat that "vehicle hp is 0" or something. But if I check it with onClientRender then it will flood the chat until my hp is 0, how can I manage to write once in the chat if the hp is reaching 0?

local msg = false

function checkVehHealth()
    local veh = getPedOccupiedVehicle(localPlayer)
    
    if veh then
        local currHealth = getElementHealth(veh)

        if currHealth <= 0 and not msg then
            outputChatBox("Your vehicle HP is 0", 255, 0, 0)
            msg = true
        elseif currHealth > 0 then
            msg = false
        end
    end
end

addEventHandler("onClientRender", root, checkVehHealth)

u can use a boolean variable to ensure the message is only displayed once

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