Code not running?

Hello, I have this code:



hook.Add( "KeyPress", "RemoveEntity", function( ply, key )
   if ( ply:GetActiveWeapon():GetClass() == "weapon_sh_tool" ) then
       if(key == KEY_1 or key == KEY_2 or key == KEY_3 or key == KEY_4)then --DOESN'T RUN!
           ents.FindByName("GhostEntity"):Remove()
       end
   end
end )


and the second if statement doesnt run, any ideas? this is being ran clientside.

ents.FindByName return a table. You are trying to remove table not an entity.

What do you suggest I use instead?

Loop through the table and remove each entry.



for k, v in pairs( ents.FindByName("GhostEntity") ) do
    v:Remove()
end


Should remove all GhostEntity’s on map.

I have done that, still doesnt work, with no errors.

The issue: https://steamcommunity.com/sharedfiles/filedetails/?id=512143425



hook.Add( "KeyPress", "RemoveEntity", function( ply, key )
   if ( ply:GetActiveWeapon():GetClass() == "weapon_sh_tool" ) then
       if(key == KEY_1 or key == KEY_2 or key == KEY_3 or key == KEY_4)then
           for k, ent in pairs( ents.FindByName("GhostEntity"))do
               ent:Remove()
           end
       end
   end
end )


im sure its GhostEntity, as in the gamemode files, this is used to remove the entity.



self.GhostEntity:Remove()


what happens is, I hold the tool gun as usual, then press 1 or 2 or 3 or 4, it will switch to the weapon corresponding with the key, and it will just switch and the floating entity will stay there.

But is the entity actually CALLED ghostentity? Run this after self.GhostEntity is called in whatever file it’s in: print( self.GhostEntity:GetClass() )

[editline]4th September 2015[/editline]

Oh, you want to remove it when the toolgun is gone? Just use the toolgun holster function.

yes, basically, if im not using the tool gun, I want it gone

[editline]4th September 2015[/editline]

The toolgun holster function is already in the files:



function TOOL:Holster()
		if IsValid( self.GhostEntity ) then 
                    self.GhostEntity:Remove() 
                end
	end
	
	function TOOL:OnRemove()
		if IsValid( self.GhostEntity ) then 
                    self.GhostEntity:Remove() 
                end 
	end
	
	function TOOL:OwnerChanged()
		if IsValid( self.GhostEntity ) then 
                    self.GhostEntity:Remove() 
                end
	end


[editline]4th September 2015[/editline]

this is what is returned from the print

class C_BaseFlex