How to reach entity from net

You guys know, we can reach the entity in entity functions like ENT:Use. However I cant reach the entity
other functions.




util.AddNetworkString("KacNPC_BuyItem") 
net.Receive("KacNPC_BuyItem", BuyItem) ----- HERE I AM DID THIS IT DIDN'T GIVE ANY ERRORS BUT ITS NOT WORKING.



function ENT:BuyItem(length, ply) ---- SO I TRIED TO ADD 'ENT:' to function for use self: for reach the entity



I hope you guys understand what I trying to explain to you.

Basically: I want to use ‘entity:EmitSound’ you can do this in ‘ENT:’ functions like ‘self:EmitSound’ however you cant do this standart functions. How can I use it?

SORRY FOR BAD ENGLISH LANGUAGE

Your net message can contain entities. When sending a net message, write the entity to the message and use that in your net.Receive.

It’s in init.lua. It can be? Can you show with code?

Show the code where you send the message

http://hastebin.com/exepoqured.lua

1, you can’t pass the BuyItem to net.Receive that way, you have to pass the table it’s defined in as well.

2, the ENT:BuyItem function expects the entity as its first argument, however, net.Receive will pass the length of the message as the first argument. So you can’t do it like this

You didnt answer my real question : how Can I use entity?
when it like:


net.Receive("KacNPC_BuyItem", function(length, ply)

...
...
...

end)

With the way you structured it you can’t, as for why I said you can’t do it like this.

You have to send the entity in the net message along with any other values you want to get.



net.Start "SomeName"
  net.WriteEntity(TheEntityYouWantToSendThisTo)
  -- Send other information here
net.Broadcast() -- Send this to everyones' clients so the entity on their clients have it 

-- In the entity file clientside

net.Receive("SomeName", function()
  local ent = net.ReadEntity()
  ent:BuyItem() -- Either read the other information in this function or do it here, doesn't matter
end)


I dont receiving this net in client, I receiving it in server.

Then do it that way?

I did it now sound is okay but its not coming from entity its coming in a home and there is nothing.

http://hastebin.com/umusebocus.lua

and I tried to change it ent:Remove() but game crashed.

[editline]26th November 2016[/editline]

I think there is a problem with sending entity with net. Although I changed “self” to “ASDASDASD” it still same.

You have to send in the same order you read. Currently you’re sending a string then the entity, but you’re reading the entity first

okay I will try it. thanks

[editline]26th November 2016[/editline]

Still same.

[editline]26th November 2016[/editline]

I read this post:
http://forum.facepunch.com/showthread.php?t=946605

So I did




local entity = nil
function ENT:Initialize( )
    entity = self
end

--in net function, I writed "entity:EmitSound("wav.wav")"



So it works. I not need this “net.writeentity” things.