How to get a prop to remove itself after taking too much damage
5 replies, posted
I'm putting the finishing touches on a custom TTT wep, all that I need it to do is remove itself from the world once it takes a certain amount of damage. The way the weapon works is that the player can left click to attach a prop to a surface, then right click to detonate the prop.
Here's the significant code
[CODE]
local PlantSound = Sound("Grenade.Blip")
function SWEP:plant_bomb(model_file)
if not self:CanPrimaryAttack() then return end
local trace = {}
trace.start = self.Owner:GetShootPos()
trace.endpos = self.Owner:GetShootPos() + self.Owner:GetAimVector() * 64
trace.mask = MASK_NPCWORLDSTATIC
trace.filter = self.Owner
local tr = util.TraceLine( trace )
if ( tr.Hit ) then
if SERVER then
local ent = ents.Create ("prop_ragdoll")
ent:SetModel("models/glowstick/stick.mdl")
if IsValid(ent) then
ent:SetPos(tr.HitPos)
ent:SetOwner(self.Owner)
ent:Spawn()
ent.OurHealth = 25
bomb = ent
ent:Activate()
ent.fingerprints = self.fingerprints
self.Owner:EmitSound( "weapons/c4/c4_plant.wav" )
self.Owner:SetAnimation(PLAYER_ATTACK1)
self.Weapon:SendWeaponAnim(ACT_VM_DRAW)
canExplode = true
self:TakePrimaryAmmo(1)
end
end
end
end
function SWEP:PrimaryAttack()
if ( !self:CanPrimaryAttack() ) then return end
self:plant_bomb("downloads/models/glowstick/stick.mdl")
self:SetNextSecondaryFire( CurTime() + 10 )
end
function SWEP:SecondaryAttack(ent)
if(canExplode == true) then
ent = bomb
local explode = ents.Create("env_explosion")
explode:SetPos(ent:GetLocalPos())
explode:SetOwner(self.Owner)
explode:Spawn()
explode:SetKeyValue("iMagnitude", "2200")
explode:Fire("Explode", 0, 0)
explode:EmitSound("BaseExplosionEffect.Sound", 400, 400)
util.BlastDamage(ent, self.Owner, ent:GetLocalPos(), 600, 50)
bomb:Remove()
canExplode = false
else
print("No bomb in world")
end
end
[/CODE]
Everything else works fine, but I can't get it to take damage and remove itself after the threshold is reached.
ent:SetHealth(100)
Then use ENT:OnTakeDamage to subtract hp
Then if ent:Health() < 0 then ent:Remove() end
[QUOTE=Robotboy655;40899060]ent:SetHealth(100)
Then use ENT:OnTakeDamage to subtract hp
Then if ent:Health() < 0 then ent:Remove() end[/QUOTE]
Should ENT:OnTakeDamage be a function?
[editline]4th June 2013[/editline]
I'm getting 'attempt to index global ENT (a nil value)
Could this be because I'm using this in a SWEP? I'm VERY new to lua and I've noticed that all of my functions are SWEP and work fine, but ENT does not.