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")
activator:giveItem( activator, self.Entity.class )


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

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

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

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

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

it’s “Player” not “player”


what’s the ply for?

It needs to be
I think. Because ‘Player’ is the object and ‘player’ is a library.


Thanks very much magical talking trashcans.


Sorry, I’m still getting the same error.


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:

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

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
      self.inventory = {}

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

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