How do I remove a server side entity

I’m new to lua for Garry’s mod, and so in order to learn, I followed the chair throwing gun tutorial on the wiki. To try and understand it more, I have been messing with what it does, and now it will copy an entity on right click and throw that entity on left. I want to know how to remove the entity which is copied, just to keep the server clean. Also any other suggestions as to improvement in the code.
Currently the code just makes a lua error “Trying to remove server entity on client!”, which makes sense, I just don’t know how to get around it, if there is a way.

SWEP.PrintName = "Entity Launcher"
SWEP.Author = "Author"
SWEP.Instructions = "Left mouse to fire object, Right click to copy object"

SWEP.Spawnable = true
SWEP.AdminOnly = false

SWEP.Primary.ClipSize = -1
SWEP.Primary.DefaultClip = -1
SWEP.Primary.Automatic = true
SWEP.Primary.Ammo = "none"

SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = "none"

SWEP.Weight = 5
SWEP.AutoSwitchTo = false
SWEP.AutoSwitchFrom = false

SWEP.Slot = 1
SWEP.SlotPos = 5
SWEP.DrawAmmo = false
SWEP.DrawCrossHair = true

SWEP.UseHands = true
SWEP.ViewModel = "models/weapons/c_toolgun.mdl"
SWEP.WorldModel = "models/weapons/w_toolgun.mdl"

local ShootSound = Sound( "Grenade.Blip" )
local model = "models/props_c17/FurnitureChair001a.mdl"

function SWEP:PrimaryAttack()
    self:ThrowChair ( model )
function SWEP:SecondaryAttack()
    if (string.EndsWith( tostring(self.Owner:GetEyeTrace().Entity:GetModel()), ".mdl" )) then
        model = tostring(self.Owner:GetEyeTrace().Entity:GetModel())

function SWEP:ThrowChair( model_file )
    self:EmitSound( ShootSound )
    if (CLIENT ) then
    local ent = ents.Create( "prop_physics" )
    if ( !IsValid( ent ) ) then
    ent:SetModel( model_file )
    ent:SetPos( self.Owner:EyePos() + (self.Owner:GetAimVector() * 16 ) )
    ent:SetAngles( self.Owner:EyeAngles() )
    ent:SetCollisionGroup( 3 )
    ent:SetPhysicsAttacker( self.Owner )
    local phys = ent:GetPhysicsObject()
    if ( !IsValid( phys ) ) then
    local velocity = self.Owner:GetAimVector()
    velocity = velocity * 10000
    velocity = velocity + (VectorRand() * 10 )
    phys:SetVelocity( velocity )
    function und()
        if ( !IsValid( ent ) ) then
    timer.Create(tostring(ent), 30, 1, und)

When you create (copy) the entity, is there a way of setting the ‘owner’? I’m thinking it always defaults to server, which is what is later preventing you from deleting it.

(not a lua coder just throwing some thoughts out there)

I’m not sureabout it, but i think you will have to create a net message and send it to the Server, which will remove it.

Correct me if am false

Well it would be best to learn to make your own entity instead of using prop _physics, then learn to use timer.simple and net.messages that way you can tell the swep to use your entity that you created and also use the net.message to get the string of the model your looking at, then use [lua]Entity:SetModel(your net message string )[/lua] on your swep under entity create, lastly you add a timer in your entity you created to remove itself however many second you want.

good luck sir, and sorry for my terrible grammar.

Your error is popping up because you’re trying to run SafeRemoveEntity on both the client and server even though it’s a serverside-only function. All of your code will be run in both states, serverside and clientside, unless you tell one of the states to ignore certain parts.

You can just change this:


to this:

if ( SERVER ) then SafeRemoveEntity(self.Owner:GetEyeTrace().Entity) end

and the client will ignore it.

[editline]4th July 2013[/editline]

For an example of optimization, you’re running


three different times within 4 lines. You can simplify that greatly by running it once and storing the entity it returns as a variable, then just referencing the variable. This won’t make a huge difference on a small scale, but it’s a necessity for more complex code.