• How do I make an entity remove itself after 10 seconds?
    14 replies, posted
I'm working on a requested project and I ran into a snag: How do I make the entity remove itself after 10 seconds? I tried using this: [lua] function enddummy() Ent:Remove() end [/lua] And then in the spawn function I did this: [lua] function ENT:SpawnFunction( ply, tr ) if ( !tr.Hit ) then return end local ent = ents.Create("ent_poop") ent:SetMaterial("models/shiny") --This works ent:SetColor(63,60,60,140) --This works too ent:SetPos( tr.HitPos + tr.HitNormal * 16 ) ent:Spawn() ent:Activate() timer.Simple( 1, enddummy ) --This is where I get an error return ent end [/lua] My error is: [CODE]Timer Error: entities/ent_poop/init.lua:73: attempt to index global 'Ent' (a nil value)[/CODE] Anyone know a fix to this? :wookie:
[lua]timer.Simple( 10, function() if IsValid( self ) then self:Remove() end end )[/lua]
As an alternative: [lua] function ENT:Initialize() --some other code self:Fire("kill", "", 10) end [/lua] But given the choice I'd probably do blackops' solution. Seems better.
[QUOTE=blackops7799;20352078]timer.Simple( 10, function() if IsValid( self ) then self:Remove() end end )[/QUOTE] This doesn't work. :frown:
Are you using a function that has the format ENT:xyz()?
I used [lua] timer.Simple( 10, function() if IsValid( self ) then self:Remove() end end ) [/lua] And it just didn't do anything. Now I'm just trying to use [lua] function ENT:Use(activator) self.Entity:Remove() end [/lua] And that doesn't even work :confused: I'm putting it in the init.lua, where it should go.
The function doesn't know what self is. Put this in your initialise or spawn function. [lua] timer.Simple( 1, function() self.Entity:Remove() end ) [/lua]
Why not use an already [url=http://wiki.garrysmod.com/?title=G.SafeRemoveEntityDelayed]existing function[/url]: [lua]SafeRemoveEntityDelayed( self, 10 );[/lua]
[QUOTE=iRzilla;20381020]You can't code for shit.[/QUOTE] I agree, but I wouldn't have put it so harshly.
Lol, thanks guys :( In my defense I only started learning coding around one month ago. WIth lua being my first language.
Ok then, what is so bad with that code I posted?
[QUOTE=sintwins;20381486]Lol, thanks guys :( In my defense I only started learning coding around one month ago. WIth lua being my first language.[/QUOTE] Most people would be far past the stage of making trivial syntax errors in Lua a month in from learning. [editline]07:31PM[/editline] [QUOTE=sintwins;20382109]Ok then, what is so bad with that code I posted?[/QUOTE] Trivial syntax errors.
At least he's trying to learn :j:
Sorry, you need to Log In to post a reply to this thread.