How to remove an entity (WAC Aircraft) after its destroyed?

I would like to make it so that once the pilot’s aircraft is finally destroyed, then the aircraft will be deleted after a short time so to conserve server resources. I know how to use simple timers and I’m assuming Entity:Remove() will be used, but I’m not quite sure what hook I can use that is called when the vehicle is destroyed, if there is one.

Any help would be appreciated.

I wrote a BF2 style respawning system for vehicles so that when they are destroyed, they respawn a short time after ( and remove the destroyed version ). Unfortunately it was a paid script ( I own rights, but it wouldn’t be right to release it ).

This was for scars, and wac. Basically it monitored the health NW Var and .disabled variable and removed the vehicle if it was disabled or out of hp:


		// Vehicle is valid, check health
		local _nwHealth = _vehicle:GetNWFloat( "health", 100 );
		local _bDisabled = _vehicle.disabled;
		if ( ( _nwHealth && _nwHealth <= 0 ) || _bDisabled ) then
			// Safely remove entity if its out of hp...
			SafeRemoveEntity( _vehicle );
		end
	

SafeRemoveEntity is like calling Entity:Remove( ) but it makes sure the entity is valid before removing ( prevents errors ).

The snippet is essentially all you’d need, plus a way to monitor damage…

I can’t recall whether or not Scars or WAC used the EntityTakeDamage / Scale Damage hooks, but If I recall correctly they don’t so you could check every 30 seconds in a timer.Create when the entity is created; when you remove the entity, remove the specifically named timer.

Thanks for helping me out again Acecool. I’ve tried to make it but I’m a little stuck, I’m not sure how to get the actual vehicle’s ID to check the health of. Here is what I have so far:

Clientside:


Spawn.DoClick = function()
	RunConsoleCommand("spawnapache")
	local TimerName = ""..tostring(LocalPlayer():UniqueID())..""..tostring(math.random(1, 100)).."" //Added random number just incase player has more than one aircraft spawned (bad way to do it, I know, feel free to give a tip on this if you like)
	timer.Create(TimerName, 30, 0, function CheckHealth(players vehicle id?, TimerName) end)	
end

function CheckHealth(ID, Timer)
local _nwHealth = ID:GetNWFloat( "health", 100 );
		local _bDisabled = ID.disabled;
		if ( ( _nwHealth && _nwHealth <= 0 ) || _bDisabled ) then
			// Safely remove entity if its out of hp...
			SafeRemoveEntity(ID );
			timer.Remove(Timer) //I'm guessing this is how I'm supposed to remove the timer, wiki page is blank...
		end
end

Server side:


concommand.Add("spawnapache", function(ply)
local Vehicle = ents.Create( "wac_hc_littlebird_h500" );

	if ( !IsValid( Vehicle ) ) then return; end

	Vehicle:SetPos(Vector(4607, 0, -8575));
	Vehicle:SetAngles(Angle(0, 90, 0)); 
	Vehicle:Spawn( );
	Vehicle:Activate( );

	ply:PrintMessage(3, "Your aircraft is on the helipad!")
end)

As you can see I’ve sort of got this mapped out but its not quite there, so if you wouldn’t mind helping me a little more I would really appreciate it!

It wouldn’t be the ID, it’d be the variable storing the entity reference.

You’d run all of the code server-side. Just add the timer server-side when you create the entity. Name it something like: “VehicleHealthCheck_” … vehicle:EntIndex( )

Then on remove just remove the same name ( easy enough to get ).

Thanks so much for helping me out again Acecool! I have everything working like I wanted now. Here is the code just in case you want to see what I’ve got now:
clientside:


Spawn.DoClick = function()
	if Helicopter == nil then 
	LocalPlayer():PrintMessage(HUD_PRINTTALK, "Please select a helicopter to spawn.")
	else
		net.Start("spawnheli")
		net.WriteString(Helicopter)
		net.SendToServer()
		
	end
end

serverside:


net.Receive("spawnheli", function(Length, ply)
local Helicopter = net.ReadString()
local Vehicle = ents.Create( Helicopter );

if ( !IsValid( Vehicle ) ) then 
	return false
else
	Vehicle:SetPos(Vector(4607, 0, -8550))
	Vehicle:SetAngles(Angle(0, 0, 0))
	Vehicle:Spawn()
	Vehicle:Activate()
	ply:PrintMessage(3, "Your aircraft is on the helipad!")
//----------------------------------------------
	local Timer = "VehicleHealthCheck_" .. Vehicle:EntIndex( )
	timer.Create(Timer, 30, 0, function(ply)
	// Vehicle is valid, check health
		local _nwHealth = Vehicle:GetNWFloat( "health", 100 );
		local _bDisabled = Vehicle.disabled;
		if ( ( _nwHealth && _nwHealth <= 0 ) || _bDisabled ) then
			// Safely remove entity if its out of hp...
			SafeRemoveEntity( Vehicle )
			PrintMessage(3, "Helicopter removed!")
		end
	end)
//----------------------------------------------
end
end)
 util.AddNetworkString( "spawnheli" )

Thanks for being so patient with me helping me out over the past few days. I’m going to continue working on this simple game mode and continue to expand on it, so I’m sure I will have more questions in the future. Without people like you helping other beginner gmod lua coders on this website, I don’t know what I would do!