DriFtyZ

[BUG] vehicle health to integer

Recommended Posts

Hello there, i want to make a function where when the vehicle damages it syncs its health to the sqlite db.. It works quite a bit but the numbers are a bit different in database and in game

Code Below:

addEventHandler("onVehicleDamage", getRootElement(),
function(loss)
	local vehHealth = math.floor((getElementHealth(source) / 10) - (loss / 10)) 
	local owner = getElementData(source, "Owner")
	local getAccName = getAccountName(getPlayerAccount(owner))
	local getPlate = getVehiclePlateText(source)
	local checkOwner = dbQuery(vehicleDb,"SELECT * FROM vehicles WHERE ownerName=? AND veh_plate=?", getAccName, getPlate)
	local checkOwnerResult = dbPoll(checkOwner, -1)
	if checkOwnerResult ~= 0 then
		dbExec(vehicleDb,"UPDATE vehicles SET veh_Health=? WHERE ownerName=? AND veh_plate=?", vehHealth, getAccName, getPlate)
	end
end)

the database connection works well but i don't know how to calculate exact vehicle health after loss to an integer number, any help appreciated!

Edited by DriFtyZ

Share this post


Link to post

Afaik the loss has already been subtract from the vehicle health.

 

This should be enough: (There is no need for convert it to integer, since a float number is more exact)

local vehHealth = getElementHealth(source)

 

Share this post


Link to post

if i don't subtract the loss it just writes a "1000" number in database no matter how many damage been done to the vehicle

Share this post


Link to post

First start with debug the variable:

📋

    iprint("vehHealth: ", vehHealth) 

 

If this is oke, then show me how you create your table.

Share this post


Link to post

if the vehicle health goes below 300 it caughts in fire, how to calculate the health in just integers 1-100 cuz i want to show it later on a gui

Share this post


Link to post

Are you sure you want to do that? Every time a collision happens you do a query and exec on the db? It's a very bad idea, you should think of a different way. Not sure what your gamemode is, but maybe only update the db when the driver leaves the vehicle?

Share this post


Link to post

i was thinking something about leaving the vehicle or despawing but i was to focused on how to make the health show correct first and then i forgot it, thank you for reminding me this

Share this post


Link to post
2 hours ago, Gourmet. said:

For me, it saves all the vehicles on my DB (including their health), when resource stops. 

This guy has the right idea, best way for good performance.

Share this post


Link to post

agreed, but if server is running for long time and a crash happens then the database would be looong behind i think

Share this post


Link to post
40 minutes ago, DriFtyZ said:

agreed, but if server is running for long time and a crash happens then the database would be looong behind i think

Well, make it so crashes doesn't happen.

If they do happen though, you could add a timer too, but not for too short period.

  • Like 1

Share this post


Link to post

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.