ApplyDamageToPed strange damage

Hi there.
I have some issues with ApplyDamageToPed.
I managed something like “car crash damage”, if you speed to high and you ramming something = you take damage

-- inside Citizen.Thread of course
driving_speed_last = 0.0
function ManageDriving()
	local ped = GetPlayerPed(-1)
	local veh = GetVehiclePedIsIn(ped, false)
	if (veh ~= 0 and DoesEntityExist(ped) and not IsEntityDead(ped) and GetVehiclePedIsIn(ped, false)) then
		local driving_speed = GetEntitySpeed(veh)*3.6
		local diff = math.abs(driving_speed_last - driving_speed)
		
		SetTextFont(0)
		SetTextProportional(1)
		SetTextScale(0.0, 0.50)
		SetTextDropshadow(1, 0, 0, 0, 255)
		SetTextEdge(1, 0, 0, 0, 255)
		SetTextDropShadow()
		SetTextOutline()
		SetTextEntry("STRING")
		AddTextComponentString("~r~Possible damage:~s~ "..string.format('%.0f', driving_speed / 250 * GetEntityMaxHealth(ped)).."/"..GetEntityHealth(ped).."/"..GetEntityMaxHealth(ped))
		DrawText(0.90, 0.80)
		
		if diff > 30 then
			local damage = tonumber(string.format("%.f", diff / 250 * GetEntityMaxHealth(ped)))
			local hp = GetEntityHealth(ped)
			print('On paper: '..hp..' - '..damage..' = '..hp - damage)
			ApplyDamageToPed(ped, damage, false)
			print('In fact: '..hp..' - '..damage..' = '..GetEntityHealth(ped))
		end

		driving_speed_last = driving_speed
	end
end

But, damage are not real
After taking 40 damage In console i have

On paper: 137 - 40 = 97
In fact: 137 - 40 = 0

So, damage are greater then need. I need to have 97 hp but i die with 0 hp. Where i made a mistake?

Sometimes it works perfect, some damage have real numbers, but last damage anyway is bigger then need

I think for some reason… 0-100 hp is death. 101-200 is the range you want to work with.

1 Like
		if hp - damage <= 100 then
			SetEntityHealth(ped, 150)
			damage = 50
		end
		ApplyDamageToPed(ped, damage, false)

Yes, if i set 149 hp and damage 50 im die, but 150 hp and 50 damage = alive
GTA is so strange

I think this can solve

local damage = tonumber(string.format("%.f", diff / 250 * math.abs(100 - GetEntityMaxHealth(ped))))
ApplyDamageToPed(ped, damage, false)

Big thanks