Remove Entity with console

Hi, i just started developing LUA codes and i need a bit of help with client and serverside.
I have tried a lot of diffrent ways but none of them worked.

Can someone make a simple code that removes the entity im looking at with a console command named “removeentity”?
Thanks!

:slight_smile:

lua_run Entity(1):GetEyeTrace().Entity:Remove()

[editline]26th August 2017[/editline]

Assuming you are Entity(1), if not, you will need a function that finds a player by name or SteamID or whatever.

I need it as a lua script, i cant use lua_run since its for all the players and not only me :slight_smile:

[editline]26th August 2017[/editline]

I got this right now and doesnt work:
https://hastebin.com/umujewaqun.lua



concommand.Add( "removeentity", function(ply)
    pickupdrone(ply)
end )

droneinbackpack = 0

function pickupdrone(ply)
    print( ply:GetEyeTrace().Entity.PrintName  )
    if ply:GetEyeTrace().Entity.PrintName == "Camera Drone" then
        if droneinbackpack == 0 then
            print("Picked up drone")
            drone = ply:GetEyeTrace().Entity
            if ( SERVER ) then
                drone:Remvoe()
            end
        end
        
        if droneinbackpack == 1 then
            print("Unpacking drone")
        end
    end
end


I get this: Trying to remove server entity on client! 989

Make the console command on the server.

[editline]26th August 2017[/editline]

Also you might be better off using Entity:GetClass instead of Entity.PrintName.

Like this?

concommand.Add( “removeentity”, function(ply)
pickupdrone(ply)
end )

concommand.Add( “pickupdrone”, function(ply)
RunConsoleCommand(“removeentity”)
end )

droneinbackpack = 0

function pickupdrone(ply)
print( ply:GetEyeTrace().Entity.PrintName )
if ply:GetEyeTrace().Entity.PrintName == “Camera Drone” then
if droneinbackpack == 0 then
print(“Picked up drone”)
drone = ply:GetEyeTrace().Entity
if ( SERVER ) then
drone:Remvoe()
end
end

    if droneinbackpack == 1 then
        print("Unpacking drone")
    end
end

end

[editline]26th August 2017[/editline]

I use getclass in other scripts, ill change it when the remove thing works :slight_smile:

Use code tags please. What you just posted doesnt really mean anything new. I cant tell from your post what realm you are running the code in. Just make sure the concommand is created server side so that it can remove entities.

Didn’t know how to use code tags, figured out now, sorry :slight_smile: Im new on facepunch :stuck_out_tongue:

Can you please show how, im really “noob” and really want to learn LUA, it would help a lot if u could make it complete and ill study the code to understand it :slight_smile:
Sorry if its too much to ask for :stuck_out_tongue:

I cant really show you unfortunately. Lua has what are called “States”, it not super important but theres one for the Server, one for the Client and one for the Menu (the main menu, not important here either). If I had to guess, I would say you are running the code client side. Which means either you are using “lua_run_cl”, “lua_openscript_cl” or the code is being executed in a client-side script (A script run in the Client Lua state). What you need to do is run the code in any script that is run on the server. Chuck your script in somewhere like “gmod/lua/autorun/server/”.

Okay, ill do it when om home in about 1 1/2 hour. Thanks for taking time to help!

Works! How will i be able to do so it prints in the players console and not the server console?

Use ply:PrintMessage(HUD_PRINTCONSOLE, "text").

I love you, if i should do it with a “chat.AddText”, how can i do it then?

No because chat is a client-side library. You would need to network it. You can use ply:ChatPrint("text") but it wont be colored, it will just be blue.

Ah okay :slight_smile: Is there any way to make it colored?

Network it and use chat.AddText.

Sorry… haha… uhm… How can i network it? :disgust:

Something like this:



-- SERVER
util.AddNetworkString("my_custom_netstring")
concommand.Add("removeentity", function(ply)
    pickupdrone(ply)
end)

droneinbackpack = 0

local function chatAddText(ply, tbl)
    net.Start("my_custom_netstring")
        net.WriteUInt(#tbl, 32)
        
        local color = color_white
        for index, data in ipairs(tbl) do
            net.WriteString(data.text)
            
            if (IsColor(data.color)) then
                color = data.color
            end
            
            net.WriteColor(color)
        end
    net.Send(ply)
end

function pickupdrone(ply)
    print( ply:GetEyeTrace().Entity.PrintName  )
    if ply:GetEyeTrace().Entity.PrintName == "Camera Drone" then
        if droneinbackpack == 0 then
            chatAddText(ply, {
                {text = "Picked ", color = Color(255, 0, 0)},
                {text = "up ", color = Color(0, 255, 0)},
                {text = "drone", color = Color(0, 0, 255)}
            })
            
            drone = ply:GetEyeTrace().Entity
            if ( SERVER ) then
                drone:Remvoe()
            end
        end
        
        if droneinbackpack == 1 then
            print("Unpacking drone")
            chatAddText(ply, {
                {text = "Unpacking ", color = Color(255, 255, 0)},
                {text = "drone", color = Color(0, 255, 255)}
            })
        end
    end
end

-- CLIENT
net.Receive("my_custom_netstring", function(l)
    local num = net.ReadUInt(32)
    local tbl = {}
    
    for i = 1, num do
        local text = net.ReadString()
        local color = net.ReadColor()
        table.insert(tbl, color)
        table.insert(tbl, text)
    end
    
    chat.AddText(unpack(tbl))
end)


Doesnt post anything in the chat