• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Quick fix, 1 minute

Status
Not open for further replies.

andu

Sold 649 scripts, 25 maps and 9 events!
Joined
Aug 7, 2009
Messages
969
Solutions
17
Reaction score
354
GitHub
olrios
Twitch
olrios
Have to learn how does self thing works in TFS 1.x+ and have to use a local function for this. Future, I'll move it to libs so no workarounds.

Lua:
local function addTreasureLoot()
    self:addItem(2195, 1)
    return true
end

function onSay(player, words, param)
    local chest = Game.createItem(1740, 1, player:getPosition())
    chest:addTreasureLoot() -- adding loot to chest

    return true
end

Code:
8: attempt to call method 'addTreasureLoot' (a nil value)

1576599359260.png
 
Last edited:
Solution
When I wrote it in the old way, and it works:
Lua:
    local function addTreasureLoot(self)
        self:addItem(2195, 1)
...
    addTreasureLoot(chest)

However I saw multiple weird things in latest TFS, where self wasn't declared in function's arguments but function was working with it.
That's why I asked you guys.
Have to learn how does self thing works in TFS 1.x+ and have to use a local function for this. Future, I'll move it to libs so no workarounds.

Lua:
local function addTreasureLoot()
    self:addItem(2195, 1)
    return true
end

function onSay(player, words, param)
    local chest = Game.createItem(1740, 1, player:getPosition())
    chest:addTreasureLoot() -- adding loot to chest

    return true
end

Code:
8: attempt to call method 'addTreasureLoot' (a nil value)

View attachment 41092
You're not able to create a class method without it being global, you need to define your custom method inside the Item table, then all item objects will have that new method available.
 
When I wrote it in the old way, and it works:
Lua:
    local function addTreasureLoot(self)
        self:addItem(2195, 1)
...
    addTreasureLoot(chest)

However I saw multiple weird things in latest TFS, where self wasn't declared in function's arguments but function was working with it.
That's why I asked you guys.
 
Solution
Because it doesn't need to be if it uses the : syntax sugar. Class.method(self) is equivalent to classObj:method() (classObj is referred to as self inside the function body). Simply adding the self keyword to a function does not make it a method either.
 
Lua:
local function Container.addTreasureLoot(self)
    self:addItem(2195, 1)
    return true
end

function onSay(player, words, param)
    local chest = Game.createItem(1740, 1, player:getPosition())
    chest:addTreasureLoot() -- adding loot to chest

    return true
end

That is what you wanted to do
 
Lua:
local function Container.addTreasureLoot(self)
    self:addItem(2195, 1)
    return true
end

function onSay(player, words, param)
    local chest = Game.createItem(1740, 1, player:getPosition())
    chest:addTreasureLoot() -- adding loot to chest

    return true
end

That is what you wanted to do
It wont work.
 
Status
Not open for further replies.
Back
Top