Table function calling itself as "self"

I have a item table Called ITEM which contains another table called Sound


ITEM.Sound = {
    male = {
        "vo/npc/male01/pain01.wav",
        "vo/npc/male01/pain02.wav",
        "vo/npc/male01/pain03.wav",
        "vo/npc/male01/pain04.wav",
        "vo/npc/male01/pain05.wav",
        "vo/npc/male01/pain06.wav",
        "vo/npc/male01/pain07.wav",
        "vo/npc/male01/pain08.wav",
        "vo/npc/male01/pain09.wav",
    },
    female = {
        "vo/npc/Alyx/ohgod01.wav",
        "vo/npc/Alyx/uggh01.wav",
        "vo/npc/Alyx/uggh02.wav",
        "vo/npc/Alyx/hurt04.wav",
        "vo/npc/Alyx/hurt05.wav",
        "vo/npc/Alyx/hurt06.wav",
        "vo/npc/Alyx/hurt08.wav",
    }
}



ITEM.OnUse = function(ply)
    ply:EmitSound(Sound(table.Random(self.Sound[ply.gender])))
    return false
end

Now when I am trying to use this item, it returns me this error:


[ERROR] path/to/gamemode/items/item.lua:31: attempt to index global 'self' (a nil value)

What am I doing wrong?

self is nil inside of ITEM.OnUse: it’s not a meta function.

How would I be calling the Sound table then from within ITEM.OnUse?

Call it outside the function with a hook, make your table there…

What addon are you using/what is ITEM referring to?

Is ITEM.OnUse = function(self, ply)
or
function ITEM:OnUse(ply) MsgN(self)

ITEM is a table created to insert items (bread, healthpacks, cars) into the game.

I tried gonzalog’s solution with extending the basefunction of OnUse. Means I added a reference back to itself, from where I call the the function OnUse.

Thanks everyone!