• Entity is running a code more then one time
    4 replies, posted
Im having problems with one of my entity's codes. here is the code: [lua] ENT.Health = 250 function ENT:OnTakeDamage(dmg) self.Health = self.Health - dmg:GetDamage() if self.Health <= 0 then local ent = ents.Create("ent1") ent:SetPos(self:GetPos( )) ent:Spawn() timer.Create( "delete"..self.Entity:EntIndex(),0.1, 1, function() self.Entity:Remove() end ) end end [/lua] When i kill the entity every thing seems to work ok, it deleted itself and spawned a new entity were it was. But i would get horrible lag when looking at this new entity. upon further investigation i found that when i had killed the entity, at least 30 of the new entity's spawned. How would i set up the function to only run Once upon death EDIT: Solved the problem 10 mins later: Got rid of the timer and it works find now final code is this if anyone wants to use something like this: [lua] ENT.Health = 250 function ENT:OnTakeDamage(dmg) self.Health = self.Health - dmg:GetDamage() if self.Health <= 0 then local ent = ents.Create("ent1") ent:SetPos(self:GetPos( )) ent:Spawn() self.Entity:Remove() end end [/lua]
New problem i cant seem to solve on my own, it might be because of the time, i don't know, but take a look anyway: [lua] ENT.aHealth = 250 function ENT:OnTakeDamage( dmginfo, dmg ) if dmginfo:GetInflictor():GetActiveWeapon() == "weapon_test" then self.aHealth = self.aHealth - dmginfo:GetDamage() if self.aHealth <= 0 then local ent = ents.Create("ent1") ent:SetPos(self:GetPos( )) ent:Spawn() ent:SetNetworkedString("Owner", "World") self.Entity:Remove() end else end end [/lua] " if dmginfo:GetInflictor():GetActiveWeapon() == "weapon_test" then" is not working for me. how can i change this so that the entity only takes damage is its hit by weapon_test?
Try this. [lua] if dmginfo:GetInflictor():GetActiveWeapon( ):GetClass() == "weapon_test" then [/lua] Should work.
[QUOTE=meka/sniper;16051517] [lua] function ENT:OnTakeDamage( dmginfo, dmg ) ... if dmginfo:GetInflictor():GetActiveWeapon():GetClass() == "weapon_test" then ... end [/lua] [/QUOTE] Fixed. You didn't GetClass, meaning you were comparing the entity of the weapon to a string.
[QUOTE=Disseminate;16055098]Fixed. You didn't GetClass, meaning you were comparing the entity of the weapon to a string.[/QUOTE] Ah ok i see now, I tryed GetClass befor but i was calling it right after GetInflictor. Works perfect thank you guys again for solving my hair pulling problems!
Sorry, you need to Log In to post a reply to this thread.