Vehicle Health

I have seen that a few people wanted a script like this so I decided to make it. I don’t know if it has been done before, but it’s only a small script so I thought that I would make one.

The script causes vehicles after they receive enough damage to ignite and briefly after explode. It’s pretty basic but it does it’s job. Place the above code into a lua file within the garrysmod/lua/autorun/server folder.

[lua]
DefaultVehicleHP = 250
DeafultCleanupTime = 180
hook.Add(“EntityTakeDamage”, “VehicleHPHook”, function( ent, inflictor, attacker, amount, dmginfo )
if not ent:IsVehicle() then
return
end
if not ent.HP then
ent.HP = DefaultVehicleHP
end
ent.HP = ent.HP - dmginfo:GetDamage()
if ent.HP <= 0 and not ent.Destroyed then
ent.Destroyed = true
ent:SetColor(0,0,0,255)
local ExplodeTime = math.random(5,30)
ent:Ignite(ExplodeTime,100)
–[[if ent:GetDriver():IsPlayer() then
ent:GetDriver():ExitVehicle()
end
if ent:GetPassenger():IsPlayer() then
ent:GetPassenger():ExitVehicle()
end
–]]
timer.Simple(ExplodeTime, function()
if not ent then return end
local explosion = ents.Create(“env_explosion”)
explosion:SetPos(ent:GetPos())
explosion:SetOwner(attacker)
explosion:Spawn()
explosion:SetKeyValue(“iMagnitude”, “1000”)
explosion:Fire(“Explode”, 0, 0)
if ent:GetDriver():IsPlayer() then
ent:GetDriver():Kill()
end
if ent:GetPassenger():IsPlayer() then
ent:GetPassenger():Kill()
end
timer.Simple(DeafultCleanupTime, function()
if not ent then return end
ent:Remove()
end)
end)
end
end)

hook.Add(“CanPlayerEnterVehicle”, “ExplodedVehicles”, function(ply, vehicle, role)
if vehicle.Destroyed then
return false
end
end)
[/lua]

If you are inside when the car ignites, can you make it so the passengers are automatically kicked out of the car?

Thanks for the code also

:O!! Thank you

Do I need to replace fdfdfd with an entity name?

No.

Update - Updated to kick players out of the vehicle when it sets on fire. Remove the --[[ to enable this or keep them there to allow players to stay in the vehicle whilst it is on fire.

Well I tried just putting the script in and it didnt work. Then i created mutltipl copies of the script and replaced fdfdfd in each script with a diff vehicle name and it did the trick so…

PS: Atm the script does work but you have to shoot the bottoms of the vehicles which is kind of hard unless the car flips, any ideas?

ent:GetPassenger returns the same as ent:GetDriver.

Well that is the models fault I’m afraid, unless anyone has any ideas?

[editline]10:31PM[/editline]

Oh, I didn’t know that, so will I only get the driver and not any passengers?

Is there a way to adjust the models “Hitbox” or something like that?

I think that the model would need to be edited to do that and that isn’t my area :slight_smile:

Is it shared?

Why would it be shared? Damage is handled on the server.

Can you set specific vehicles HP?

Anyway to make the vehicle go away after the explosion, rather than leave an undrivable black husk.

[lua]
DefaultVehicleHP = 250
DeafultCleanupTime = 180
hook.Add(“EntityTakeDamage”, “VehicleHPHook”, function( ent, inflictor, attacker, amount, dmginfo )
if not ent:IsVehicle() then
return
end
if not ent.HP then
ent.HP = DefaultVehicleHP
end
ent.HP = ent.HP - dmginfo:GetDamage()
if ent.HP <= 0 and not ent.Destroyed then
ent.Destroyed = true
ent:SetColor(0,0,0,255)
local ExplodeTime = math.random(5,30)
ent:Ignite(ExplodeTime,100)
–[[if ent:GetDriver():IsPlayer() then
ent:GetDriver():ExitVehicle()
end
if ent:GetPassenger():IsPlayer() then
ent:GetPassenger():ExitVehicle()
end
–]]
timer.Simple(ExplodeTime, function()
if not ent then return end
local explosion = ents.Create(“env_explosion”)
explosion:SetPos(ent:GetPos())
explosion:SetOwner(attacker)
explosion:Spawn()
explosion:SetKeyValue(“iMagnitude”, “1000”)
explosion:Fire(“Explode”, 0, 0)
if ent:GetDriver():IsPlayer() then
ent:GetDriver():Kill()
end
if ent:GetPassenger():IsPlayer() then
ent:GetPassenger():Kill()
end
if not ent then return end
ent:Remove()
end)
end
end)

[/lua]

I see a mispell that ruins the code in OP and right above me :slight_smile:

Look at second line. It says

[lua]
DeafultCleanupTime = 180
[/lua]

when it should be:

[lua]
DefaultCleanupTime = 180
[/lua]

EDIT
Holy fuck, even though this has nothing to do with this topic (sort of ) I was posting and when I was done I accidentally did something with my mouse and now the whole facepunch text’s are fucked, they are like… scrolled out or something I can barely even read what I say.

Press Ctrl + 0 to reset display zoom.

This sounds cool, I don`t see the download link…

There is no download link, read the OP once more.