ENT is number and therefore can't be indexed

I don’t understand this thing, I’m creating a SENT and when the user presses E on it a menu Derma menu should show up, so I decided to use a net message to trigger the function on client-side that would create the menu. The function is being correctly called, the problem is what is happing inside it, check this code snippet:

File: cl_init.lua


function ENT:Initialize()
	MsgN(self.items)
end

function ENT:openMenu(len, ply)
	self.items = net.ReadTable()
end
//net.Receive(ENT.openMenu, ENT.openMenu) ignore this line
net.Receive(ENT.netOpenMenu, ENT.openMenu) 

File: init.lua



function ENT:Use(activator, caller, useType, value)
	net.Start(self.openMenu)
	net.WriteTable(self.items)
	net.Send(self:Getowning_ent())
end

When I press E on it the net message is correctly sent to the client-side and the function openMenu is called, but I get the following error on it:


[ERROR] addons/cozinheiro/lua/entities/fogao/cl_init.lua:74: attempt to index local 'self' (a number value)
  1. func - addons/cozinheiro/lua/entities/fogao/cl_init.lua:74
   2. unknown - lua/includes/modules/net.lua:30

It makes no sense to me, as when Initialize is called on client-side and it correctly runs the MsgN(self.items) that prints the table items, as you can see it’s also indexing self, but no errors occurs.

Do you know why it’s happening?

It’s erroring because ENT:openMenu is being called from net.Receive with length as the first argument. Thus, it’s being used as ‘self’.

-snip-

Sorry, I edited the code snippet to paste here and because of that I edited that for some reason, it’s net.Receive(ENT.netOpenMenu, ENT.openMenu) actually.

It doesn’t change why it is erroring. You can’t use self in a net.Receive function, you gotta send the entity and use it as self.

Thank you Robotboy, I thought as the function was defined inside the ENT table I would have access to it with the self keyword even inside a net.Receive function, although that doesn’t seem to be how it works.

To fix it I had to define a local variable out side the function’s scope and set its value to self.items, local items = self.items, unfortunately.

Do you know if there is any way I can refer to ENT as self inside a net.Receive function?

Yes, you would need to send the entity in the net message.

[LUA]
function ENT:Use(activator, caller, useType, value)
net.Start(self.openMenu)
net.WriteEntity(self)
net.WriteTable(self.items)
net.Send(self:Getowning_ent())
end

function ENT:openMenu(len, ply)
local self = net.ReadEntity()
self.items = net.ReadTable()
end
net.Receive(ENT.openMenu, ENT.openMenu)
[/LUA]

That should work.