Jump to content

[QUESTION] why onVehicleDamage event doesn't trigger always?


DriFtyZ

Recommended Posts

As the title says onVehicleDamage doesn't register always.. what i mean is i have the following code
 

addEventHandler("onVehicleDamage", getRootElement(),
function(loss)	
	local health = getElementHealth(source) - 250
	local vehHealth = math.floor(health / 750 * 100)
	if vehHealth < 1 then
		setElementHealth(source, 250)
	end
	iprint("vehHealth: ", vehHealth)
end)

i have set the vehicle health showing as an integer from 1 to 100 and when it reaches below 0 it caughts in fire but when the vehicle is 0 and caughts up in fire sometimes the event works and restores the health to 250 which is 0(before it caught in fire) and sometimes not and the vehicle explodes. Does anybody knows why this happens

Link to comment

onClientVehicleDamage seems to be more accurate but theres one thing that confuses and it happend on both events when the vehicle is 100 and hits a wall for first time it doesn't get his health and i have to hit again to start decreasing it(in script)

Link to comment
  • Moderators
local damageTimeCount = 0
addEventHandler("onVehicleDamage", getRootElement(),
function(loss)
	damageTimeCount = damageTimeCount+1
	local health = getElementHealth(source) - 250
	local vehHealth = math.floor(health / 750 * 100)
	if vehHealth < 1 then
		setElementHealth(source, 250)
	end
	iprint("damageTimeCount: " .. damageTimeCount .. ", vehHealth 1: ", vehHealth)
	setTimer(function (vehicle, damageTimeCount)
		local health = getElementHealth(vehicle) - 250
		local vehHealth = math.floor(health / 750 * 100)
		iprint("damageTimeCount: " .. damageTimeCount .. ", vehHealth 2: ", vehHealth)
	end, 50, 1, source, damageTimeCount)
end)

What is the result when you use this code?

 

Edited by IIYAMA
  • Like 1
Link to comment

vehHealth 2 on iprint is correct, wait imma modify this into a client side function i did before

works like a charm right now, in speedometer it always show current health because its getting refreshed in every HUD rendered frame but in script it just stayed 1 health variable behind and seems the 50ms delay gets it fixed

-- Sync vehicle damage from client to server
local damageTimeCount = 0

addEventHandler("onClientVehicleDamage", getRootElement(),
function(attacker, weapon, loss, x, y, z, tyre)	
	damageTimeCount = damageTimeCount + 1
	if attacker or weapon then
		cancelEvent()
	end
	local vehElement = source
	local vehPlate = getVehiclePlateText(source)
	local player = getElementData(source, "player")
	
	setTimer(function (vehicle, damageTimeCount)
		local health = getElementHealth(vehicle) - 250
		local vehHealth = math.floor(health / 750 * 100)
		iprint("damageTimeCount: " .. damageTimeCount .. ", vehHealth 2: ", vehHealth)
		triggerServerEvent("healthToDb", getRootElement(), vehHealth, vehElement, vehPlate, player)
	end, 50, 1, source, damageTimeCount)
end)

 

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