• Remove entity find in sphere and by class
    10 replies, posted
Hi everybody i have a small question that would help me greatly: how to delete a specific entity with a radius ? (with ents.FindInSphere and ents.FindByClass) I have this code but it removes any entity: ents.FindByClass( "ml_cow" )[1]:Remove() Thank you ^^
In radius of what? In radius of a certain point on a map or in radius of an entity/player?
In radius of a entity ^^ I want to remove my cow when his body disappears (it's in two entities)
I have try this but he doesn't work (sorry i'm a novice): local radius = 1 if( ents.FindByClass("ml_vache"):GetPos():Distance(self) <= radius ) then ents.FindByClass( "ml_vache" )[1]:Remove() end [ERROR] addons/ml_fermier/lua/entities/ml_vache_corps/init.lua:53: attempt to call method 'GetPos' (a nil value)   1. unknown - addons/ml_fermier/lua/entities/ml_vache_corps/init.lua:53
You're calling Entity:GetPos() on a table. You should use a for loop for this. Lua 5.3 Reference Manual
I have tried this: for k, v in pairs( ents.FindByClass( "ml_vache" ) ) do  local pos = v:GetPos()  if ( pos:Distance(self:GetPos()) <= 1 ) then  ents.FindByClass( "ml_vache" ):Remove()  end end but he doesn't work (i don't have any errors in the console)
You're still using a table as an entity. ents.FindByClass returns a table of found entities with the class name, not an actual entity.
I am obliged to use a table, my two entities are separated (one is an nextbot, the other a prop_physics)
[code]for k, v inpairs( ents.FindByClass( "ml_vache" ) ) dolocal pos = v:GetPos() if ( pos:Distance(self:GetPos()) <= 1 ) then  --This is calling remove on a table of entities rather than the one you're checking ( v ) --ents.FindByClass( "ml_vache" ):Remove() --You want this instead v:Remove(); end end[/code]
It also helps to name your variables properly. local origin = self:GetPos() for i, ent in ipairs(ents.FindByClass("ml_vache")) do if (origin:Distance(self:GetPos()) <= 1) then ent:Remove() end end
The code does not work but I feel that we reach the end ^^ this is the script of "ml_vache_corps", it's the entity/body attach with the nextbot AddCSLuaFile("cl_init.lua") AddCSLuaFile("shared.lua") include("shared.lua") function ENT:Initialize()     self:SetModel("models/hunter/blocks/cube1x2x05.mdl") self:SetMaterial ("Models/effects/vol_light001")     self:PhysicsInit(SOLID_VPHYSICS)     self:SetMoveType(MOVETYPE_VPHYSICS)     self:SetSolid(SOLID_VPHYSICS)     self.Replace = false     self:SetNWInt("health", 10)     self:SetNWInt("distance", 512) local function VacheSounds() self:EmitSound("farmingmod/cow1.mp3") end timer.Create( "Vache Sounds", math.random(10,30), 0, VacheSounds ) end function ENT:Think() if (!self.Replace) and (self:GetNWInt("health") <= 0)  then local viandes = math.Rand(3, 3, 4) for i=1, math.Round(viandes) do local viande = ents.Create("ml_viande_vache_cru") viande:SetPos(self:GetPos() + Vector(math.Rand(1,20), math.Rand(1,20),20)) viande:Spawn() end self.Replace = true self:Remove() end; end function ENT:OnTakeDamage(dmg) self:VisualEffect(); self:StartLoopingSound("farmingmod/cow1.mp3") if table.HasValue({"ml_axe"}, dmg:GetInflictor():GetClass()) or table.HasValue({"ml_axe"}, dmg:GetAttacker():GetActiveWeapon():GetClass()) then self:SetNWInt("health", self:GetNWInt("health")-1) end end function ENT:OnRemove() if not IsValid(self) then return end timer.Remove( "Vache Sounds" ) local origin = self:GetPos() for i, ent in ipairs(ents.FindByClass("ml_vache")) do  if (origin:Distance(ent:GetPos()) <= 5) then ent:Remove() end  end end function ENT:VisualEffect() local effectData = EffectData(); effectData:SetStart(self:GetPos()); effectData:SetOrigin(self:GetPos()); effectData:SetScale(8); util.Effect("BloodImpact", effectData, true, true); end; The "ml_vache" before his death (with ml_vache_corps) https://files.facepunch.com/forum/upload/271242/bbd56793-75eb-482c-981a-599fe9b922d2/Screenshot_123.png The "ml_vache" after his death (without the "ml_vache_corps") https://files.facepunch.com/forum/upload/271242/4b68a4cc-d56a-445d-820b-6eb6e755d0c9/Screenshot_124.png
Sorry, you need to Log In to post a reply to this thread.