DriFtyZ Posted June 3, 2017 Share Posted June 3, 2017 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
idarrr Posted June 3, 2017 Share Posted June 3, 2017 I had this problem before, it seems that event doesn't trigger when player attack that vehicle, so you need to use onClientVehicleDamage. Link to comment
Moderators IIYAMA Posted June 3, 2017 Moderators Share Posted June 3, 2017 That has probably to do with the bandwidth saving (and maybe also finding it hard to decide which player should sync the vehicle). Link to comment
DriFtyZ Posted June 3, 2017 Author Share Posted June 3, 2017 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 IIYAMA Posted June 3, 2017 Moderators Share Posted June 3, 2017 (edited) 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 June 3, 2017 by IIYAMA 1 Link to comment
DriFtyZ Posted June 3, 2017 Author Share Posted June 3, 2017 (edited) 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 June 3, 2017 by DriFtyZ Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now