Function isn't getting called

I’m sure this is a simple problem which I’m missing due to lack of sleep, but meh. When running this piece of code:
[lua]
print(“adding a mot:”,Thing1,Thing2)
if Thing1.IsRDAMotor then Thing1:AddMotivator( Thing2 )
else Thing2:AddMotivator( Thing1 ) end
[/lua]
Towards a SEnt with this code:
[lua]
function ENT:AddMotivator( ent )
print(“motor adding mot”,ent)
end
[/lua]
Lua is getting lost somewhere. I get the first print message, but I can’t get the second one. I’m also not getting any errors in the console. What am I doing wrong?

  1. Check if the file you got your script on is included correctly.
  2. Check if there’s any lua errors in console (entity errors are usually far behind in the console).
  3. Check if you got your both your script serverside/clientside, or maybe shared.

Ps. Are you really calling the right entity? Are you sure that “Thing1 and Thing2” are entities with that script?

  1. Yep. Other parts of the script are running just fine.
  2. None at all.
  3. They’re both serverside.

PS) According to the print statement which tells me what Thing1 and Thing2 are, yes.

Seriously, I can’t run this function no matter what. I don’t understand! Even if I use this console command:



lua_run for i, e in pairs(ents.FindByClass("rda_base_motor")) do for k, v in pairs(ents.FindByClass("gmod_thruster")) do print("!") e:AddMotivator(v) end end


I’ll get the "!"s, but not the rda_base_motor’s “motor adding mot” message… And no errors!

When you do an ENT:lolfunction function, it’s not actually stored in the entity, but rather the table you get when you do lolentity:GetTable().

So to correctly call that AddMotivator function do (in the case of your lua_run code) e:GetTable():AddMotivator(v)

This is not true.

Lexic is correct - GetTable( ) is defunct. The __index function refers to the entity itself, and it’s been like this for a long time now ( I don’t remember the specific update ).

I still use ent:GetTable when I’m debugging and want to PrintTable the vars stored in the table.

I still can’t get it working, so I went added more debug data just for the hell of it:
[lua]print(“tool adding a mot:”,ent1,ent2)
print(ent1.AddMotivator, ent2.AddMotivator)
PrintTable( ent1:GetTable() )[/lua]
Lo and behold, it reported:


tool adding a mot:	Entity [107][rda_base_motor]	Entity [103][gmod_thruster]
function: 035742D8	nil
[lots and lots of crap]
AddMotivator	=	function: 035742D8

So that function is /certainly/ there! So why won’t it do anything???

[editline]08:26PM[/editline]

Wait, wait, disregard this thread. Christ I’m dumb. Hiding way down later in the script:
[lua]-- Remove a motivator from the motor
function ENT:AddMotivator( ent )
end[/lua]

Problem solved. And that, children, is why you shouldn’t code on less than four hours of sleep.