Ok so I've got a script that destroys vehicles and removes them....
Only problem is that when they are destroyed I seem to get an error
Error:
[QUOTE]Hook 'entityTakeDamage' Failed: [addons\test\lua\autorun\damage.lua:15] attempt to index field '?' (a nil value)[/QUOTE]
The script is this:
[lua]cars = {}
function SpawnedVehicle( PlayerEntity, VehicleEntity )
vehicle_index1 = VehicleEntity:EntIndex()
print( vehicle_index1 )
cars[vehicle_index1] = {}
cars[vehicle_index1].entity = VehicleEntity
cars[vehicle_index1].damage = .1
end
hook.Add( "PlayerSpawnedVehicle", "playerSpawnedVehicle", SpawnedVehicle )
function CarDamage( VehicleEntity, EntityWeapon, EntityAttacker, EntityDamage )
if ( VehicleEntity:IsVehicle() ) then
vehicle_number = VehicleEntity:EntIndex()
if ((cars[vehicle_number].damage - EntityDamage) < .0001) then
local explode = ents.Create( "env_explosion" )
explode:SetPos( VehicleEntity:GetPos() )
explode:SetOwner( EntityAttacker )
explode:Spawn()
explode:SetKeyValue( "iMagnitude", "120" )
explode:Fire( "Explode", 0, 0 )
explode:EmitSound( "weapon_AWP.Single", 400, 400 )
VehicleEntity:Remove()
elseif( (cars[vehicle_number].damage - EntityDamage) > 0 ) then
print( cars[vehicle_number].damage )
cars[vehicle_number].damage = (cars[vehicle_number].damage - EntityDamage)
end
end
end
hook.Add( "EntityTakeDamage", "entityTakeDamage", CarDamage )
[/lua]
Any help would be appreciated, thankya!
I believe it has something to do with the .0001, change that to 1.
Or the [vehicle_number] is a nil value.
The vehicle number becomes nil because the vehicle is destroyed.
And that is what causes the problem. Why is it still counting the damage after the vehicle gets destroyed?
And the damage needs to be quite low because frankly, metal is quite strong. I waste about 4 smg clips on the car before it finally blows up.
Hm, alright. Well I'm pretty new to Lua, so this is about all the help I can give, sorry!
Although I know there is a script out there, somewhere S:
That's fine, thanks for the help at least.
Anyone else out there who might be able to figure that ^^^ out?
Found you something ;)
[url]http://www.facepunch.com/threads/981311-Vehicle-Health[/url]
Oi that was helpful thankyou, great learning tool indeed, I was being more complicated than necessary. XD
Here's the catch with that script, it only works on some cars cause of where "vehicle" is stated in the Model. Of course, with my amazing know-how, I just fixed it :D
Edit: I'll PM you my finished product, don't feel like giving any precious code, that I spend hours on-- given away.
Edit: Nope, dun wurkz
Why don't you make a variable on vehicle spawn and then do checks in the entitytakedamage hook to see if the entity is a prop_vehicle_jeep and then if the health is <= 0 do your explosion things.
[lua]
hook.Add("PlayerSpawnVehicle","Caryo",function(player,car)
car.Health = 100
end)
hook.Add("EntityTakeDamage","Checkyo",function(ent,wep,atk,amt)
if ent:GetClass() == "prop_vehicle_jeep" then
if ent.Health <= 0 then
--do your explosion stuff here.
end
end
end)
[/lua]
I can't tab as I'm on a shitty laptop with no Npp :<
[QUOTE=jrj996;31562512]Why don't you make a variable on vehicle spawn and then do checks in the entitytakedamage hook to see if the entity is a prop_vehicle_jeep and then if the health is <= 0 do your explosion things.
[lua]
hook.Add("PlayerSpawnVehicle","Caryo",function(player,car)
car.Health = 100
end)
hook.Add("EntityTakeDamage","Checkyo",function(ent,wep,atk,amt)
if ent:GetClass() == "prop_vehicle_jeep" then
if ent.Health <= 0 then
--do your explosion stuff here.
end
end
end)
[/lua]
I can't tab as I'm on a shitty laptop with no Npp :<[/QUOTE]
Tried that, and only the Seat of the Jeep takes damage. The rest of the model takes no damage.
Sorry, you need to Log In to post a reply to this thread.