player metaTable errors

Alright, so I tried to add more functions to the metaTable “player”, so I could start on an inventory of sorts. However, whenever I try to call the method on an entity, as a use function, it returns an error.
Ent code:
[lua]function ENT:Use( activator, caller)

Msg("it's alive! :O")
Msg(self.Entity.class)
activator:giveItem( activator, self.Entity.class )

end[/lua]

MetaTable code:
[lua]local meta = FindMetaTable(“player”)

function meta:takeItem( ply, class)
self.inventory[class] =- 1
Msg(“You’ve just lost”…class)
end

function meta:giveItem( ply, class)
self.inventory[class] =+ 1
Msg(“You’ve just recieved”…class)
end

function meta:inventoryToString( ply )
local parse = “”
for k, v in pairs ( self.inventory ) do
parse = parse…"+"…k…"="…v
end
return parse
end[/lua]

Exact error is “entities/item/init.lua:25: attempt to call method ‘giveItem’ (a nil value)”

it’s “Player” not “player”

[editline]03:58PM[/editline]

what’s the ply for?

It needs to be
FindMetaTable(“Player”)
I think. Because ‘Player’ is the object and ‘player’ is a library.

EDIT:
ninja’d

Thanks very much magical talking trashcans.

[editline]03:06PM[/editline]

Sorry, I’m still getting the same error.

[editline]03:10PM[/editline]

Still broked. Same error.

Are you absolutely sure the metatable code is being run? What’s it being called from - the entity script? Autorun/server? Something else? Did you check the console for any errors relating to the metatable script itself?

Why are you passing the player trough the argument when you are using self?

Do it like this:

[lua]
function _R.Player.takeItem( ply, class )
ply.inventory[ class ] = ply.inventory[ class ] - 1
ply:ChatPrint("You’ve just lost " … class )
end
[/lua]

I see now that the ply arguement was extra, but I’m positive the metatable is being run, I’m running it through a gamemode.

@combineguru - explanation plox.

I’ll look for a metatable related error.

There is no such lua operation =+. Because of this, your metatable code isn’t being run and therefore you are not adding the functions.

I can also see potential errors in that you never defined self.inventory as a table.

How would I go about defining a table within a meta table?

And yes Lexi, that’s been changed since then. :expressionless:

Just say


self.inventory = self.inventory or {}

In Lua, a “nil” value is considered “false” in an or statement. So that statement will evaluate is the same as


if self.inventory != nil then
      self.inventory = self.inventory
else
      self.inventory = {}
end

That way if the inventory is nil, then you get an empty table.

Oh, I guess I’ll try that. Thank you very much.