• If key pressed, remove entity
    2 replies, posted
Hello, I want to remove an entity when a key for example KEY_1 is pressed, I found some code that I can use to achieve this. [code] function TOOL:Holster() if IsValid( self.GhostEntity ) then self.GhostEntity:Remove() end end [/code] I want to be able to use that code to run self.GhostEntity:Remove() when a key is pressed.
[code]hook.Add( "KeyPress", "RemoveEntity", function( ply, key ) if ( key == KEY_1 ) then -- remove entity here. self will not work since there is no parent; you'll probably need to use ents.Find end end )[/code]
[QUOTE=code_gs;48613850][code]hook.Add( "KeyPress", "RemoveEntity", function( ply, key ) if ( key == KEY_1 ) then -- remove entity here. self will not work since there is no parent; you'll probably need to use ents.Find end end )[/code][/QUOTE] Am I right in thinking I should use ents.FindByName("GhostEntity")? [editline]4th September 2015[/editline] At the moment I have this code: [code] hook.Add( "KeyPress", "RemoveEntity", function( ply, key ) if ( key == KEY_1 or key == KEY_2 or key == KEY_3 or key == KEY_4 ) then print(ents.FindByName("GhostEntity")) ents.FindByName("GhostEntity"):Remove() end end ) [/code] I get the error that remove is a nil value. A screenshot of my issue: [url]http://steamcommunity.com/sharedfiles/filedetails/?id=512143425[/url] im not sure what im doing is the best way to solve it, but all I basically want to do, is make that object get removed, when I switch to a weapon (basically, press KEY_1 etc)
Sorry, you need to Log In to post a reply to this thread.