Removing server entity from client.

Hello, I have this code:



ply = LocalPlayer()
 
hook.Add( "Think", "Every Frame", function()
   if(ply:GetNWInt('spawn', 0) == 1)then
      if(ply:GetActiveWeapon():GetClass() == "weapon_sh_tool")then
           if(input.IsKeyDown(KEY_1) or input.IsKeyDown(KEY_2) or input.IsKeyDown(KEY_3) or input.IsKeyDown(KEY_4))then
               ply:GetEyeTrace().Entity:Remove()
           end
      end
   end
end)


and it just gives the error, tried to remove server entity from client. so I was wondering how I can receive when I want it removed on the server, then remove it.

OMG! STOP CREATING THREADS. Use net library to send the entity and remove it in serverside.

You will have to use the net library. I haven’t used it before so I can’t give much information, but in dumbed down form you need a sender and a receiver. In your above code where you try to remove the entity, you would send a net message containing the entity you want to remove. The server would then have a net receiver that would take that entity and remove it.

Cant help needing loads of help in different subjects

[editline]5th September 2015[/editline]

Is this correct?



ply = LocalPlayer()
 
hook.Add( "Think", "Every Frame", function()
   if(ply:GetNWInt('spawn', 0) == 1)then
      if(ply:GetActiveWeapon():GetClass() == "weapon_sh_tool")then
           if(input.IsKeyDown(KEY_1) or input.IsKeyDown(KEY_2) or input.IsKeyDown(KEY_3) or input.IsKeyDown(KEY_4))then
               net.Start( "Ghostent" )
               net.WriteEntity( ply:GetEyeTrace().Entity )
               net.SendToServer()
           end
      end
   end
end)


Don’t trust the client and verify that it is the correct entity before removing it, but I believe that will work as long as there is a valid entity.

It will be the correct entity always, as the entity is set o the players EyeTrace

A person could use hacks to send another entity which you don’t want removed through the net library.

Why don’t you call it on the server instead? ply:GetEyeTrace().Entity works on server as well.

also, is this correct to receive the entity?



net.Receive("Ghostent", function(ply)

   net.ReadEntity():Remove() 

end)


Different subjects? What are you talking about? Every single thread has the same code, where you try to fix bug in the stronghold gamemode. You can just ask in same thread and not to spam fp with same threads.

Try SafeRemoveEntity
http://wiki.garrysmod.com/page/Global/SafeRemoveEntity

Just use

GM/PlayerButtonDown instead of a Think hook and the input library and you can do this all serverside. If you want an example of how this could be abused:



RunThisLuaOnClient([[
	for i, v in pairs(player.GetAll()) do
		net.Start('Ghostent')
			net.WriteEntity(v)
		net.SendToServer()
	end
]]) -- rip server


Yes, but as I have said before you should verify that it’s the entity you want removed or as darkjacky suggested do it serverside.

Check if the entity’s class is the ghost ent, if it is delete it otherwise do nothing.

also, is this correct to receive the entity?



net.Receive("Ghostent", function(ply)

   net.ReadEntity():Remove() 

end)


I am now running this serverside, and will be testing it:



hook.Add( "PlayerButtonDown", "Check button press", function(ply,key)
   while(spawn == 1)do
      if(ply:GetActiveWeapon():GetClass() == "weapon_sh_tool")then
           if(input.IsKeyDown(KEY_1) or input.IsKeyDown(KEY_2) or input.IsKeyDown(KEY_3) or input.IsKeyDown(KEY_4))then
               ply:GetEyeTrace().Entity:Remove()
           end
      end
   end
end)


spawn is defined in the playerspawn hook

Okay, here’s what’s wrong:

  1. Why in gods name are you using a while loop like that. There’s no need for it there.
  2. The input library is clientside - you need to be checking if the key equals KEY_1 or KEY_2 and so on.
  3. You still are not checking if the entity is the class you want them to be able to remove. With that code, you could remove the player entities, delete other people’s items, etc.

Ive tested the script, and the server will let me join, but when I spawn, I will walk for like 2 seconds and it will freeze. then I lose connection and it says my server is using 12% cpu, which is a lot more than usual

Look at problem number one to see what causes that.

im running it serverside, so players shouldnt be able to change stuff like that, should they? What is the best code to use?

Ive been trying to solve this for days now, I just really need help :slight_smile:

You can delete any entity that you look at with that code (if it worked, that is). You need to have checks to make sure it is the entity you want them to be able to delete. The while loop you have is basically an infinite loop as long as spawn stays at 1, so it is crashing your server. The input library is only clientside, and the key that is pressed is passed to the PlayerButtonDown hook. With this information, make the necessary edits to the code.