Referencing Entity from Function

I’m making a SENT which has multiple functions, and I need to be able to have it delete itself from a function. I’m defining the entity in its spawn function, and the function which deletes it can’t be an ENT:abc function. How can I reference the entity if I want to use it with the Entity.Remove function?

YourEntity:Remove()

Did you even bother to READ the page?

That’s not what he’s asking. He doesn’t know what to use for “YourEntity” because ENT:Remove() and self:Remove() wouldn’t work. One can’t reference the entity object in a standard spawn function because of its scope.



local myEnt

function x()
 myEnt = ents.Create("blah")
 myEnt:SetPos(0, 0, 0)
 myEnt:Spawn()
end

function y()
 myEnt:Remove()
end

- OR -
(NOTE: This one doesn't need the functions in the same script)

function x()
 for k, v in pairs(ents.FindByClass("class of your entity")) do
  if v:Something == x then
   RemoveMyEntity(v)
  end
 end
end

function RemoveMyEntity(MyEntity)
 MyEntity:Remove()
end


Just put those in the same script and it should work. (Provided that you edit the stuff correctly ofc)

I can confirm that this works. Thanks!

Genius… it is sitting RIGHT ON THE PAGE.
The example is exactly what he wants.