• 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!

Lua Attempt to call field 'getResult' <a nil value>

8408323

Hoster
Joined
Mar 6, 2009
Messages
432
Reaction score
26
Hello,

I'm trying to create a script which can load the players items. I get this error in the console.
Code:
Attempt to call field 'getResult' <a nil value>
This is the code,
Code:
function getItemsInContainer(cont, sep)
    local text = ""
    local tsep = ""
    local count = ""
    for i=1, sep do
        tsep = tsep.."-"
    end
    tsep = tsep..">"
    for i=0, getContainerSize(cont.uid)-1 do
        local item = getContainerItem(cont.uid, i)
        if isContainer(item.uid) == FALSE then
            if item.type > 1 then
                count = " ("..item.type.."x)"
            end
            text = text.."\n"..tsep..getItemName(item.itemid)..""..count.." ("..item.itemid..")"
        else
            if getContainerSize(item.uid) > 0 then
                text = text.."\n"..tsep..getItemName(item.itemid).." ("..item.itemid..")"
                text = text..getItemsInContainer(item, sep+2)..""
            else
                text = text.."\n"..tsep..getItemName(item.itemid).." ("..item.itemid..")"
            end
        end
    end
    return text
end

function onSay(cid, words, param)
    if(param == '') then
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Command param required.")
        return false
    end
    local slotName = {"Head", "Amulet", "Backpack", "Armor", "Right Hand", "Left Hand", "Legs", "Feet", "Ring", "Ammo Slot"}
    local t = param:split(', ')
        if getPlayerGroupId(cid) >= 3 then
            id = getItemIdByName(t[1])
            if not id then
                doPlayerSendCancel(cid, "The "..t[1].." not exist. ")
                return false
            end
            local resultID = db.storeQuery("SELECT name FROM players WHERE id IN (SELECT player_id FROM player_items WHERE itemtype = ".. id.. ");")
            local msg = "Search results by item ".. getItemName(id) .." in your database:\n\n"
            if resultID then
                while true do
                    local name = result:getDataString(resultID, 'name')
                    msg = msg .. 'name' .."\n"
                    if not resultID then
                        break
                    end
                end
            else
                msg = msg .. "The item was not found in any player."
            end
            doShowTextDialog(cid, id, msg)
        end
    end
    return true
end
and I've added this into global.lua
Code:
-- Check player --
    function getPlayerNameByGUID(guid)
        local name = 0
            local resultId = db.getResult("SELECT `name` FROM `players` WHERE `id` = " .. guid)
            if(resultId:getID() ~= -1) then
            name = resultId.getDataString(resultId, "name")
                resultId:free()
            end
            return name
    end
    -- End Check player --
Somehow I can not make the script to load "db.getResult". Anyone know how I can load the db into the lua?
I've tried adding this into global.lua as well, which is kind of useless though. After adding this I shouldn't need to write db.storeQuery, right?
Code:
db.getResult = db.storeQuery

Thanks in advice,
8408323
 
Last edited:
I'm using tfs 1.0
Code:
function getItemsInContainer(cont, sep)
    local text = ""
    local tsep = ""
    local count = ""
    for i=1, sep do
        tsep = tsep.."-"
    end
    tsep = tsep..">"
    for i=0, getContainerSize(cont.uid)-1 do
        local item = getContainerItem(cont.uid, i)
        if isContainer(item.uid) == FALSE then
            if item.type > 1 then
                count = " ("..item.type.."x)"
            end
            text = text.."\n"..tsep..getItemName(item.itemid)..""..count.." ("..item.itemid..")"
        else
            if getContainerSize(item.uid) > 0 then
                text = text.."\n"..tsep..getItemName(item.itemid).." ("..item.itemid..")"
                text = text..getItemsInContainer(item, sep+2)..""
            else
                text = text.."\n"..tsep..getItemName(item.itemid).." ("..item.itemid..")"
            end
        end
    end
    return text
end

function onSay(cid, words, param)
    if(param == '') then
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Command param required.")
        return false
    end
    local slotName = {"Head", "Amulet", "Backpack", "Armor", "Right Hand", "Left Hand", "Legs", "Feet", "Ring", "Ammo Slot"}
    local t = param:split(', ')
        if getPlayerGroupId(cid) >= 3 then
            id = getItemIdByName(t[1])
            if not id then
                doPlayerSendCancel(cid, "The "..t[1].." not exist. ")
                return false
            end
            local resultID = db.getResult("SELECT name FROM players WHERE id IN (SELECT player_id FROM player_items WHERE itemtype = ".. id ..");")
            local msg = "Search results by item ".. getItemName(id) .." in your database:\n\n"
            if resultID:getID() ~= -1 then
                while true do
                    local name = resultID:getDataString("name")
                    msg = msg .. name .."\n"
                    if not resultID:next() then
                        break
                    end
                end
            else
                msg = msg .. "The item was not found in any player."
            end
            doShowTextDialog(cid, id, msg)
        end
    end
    return true
end
Updated main post whit whole script
 
I'm using tfs 1.0
Code:
function getItemsInContainer(cont, sep)
    local text = ""
    local tsep = ""
    local count = ""
    for i=1, sep do
        tsep = tsep.."-"
    end
    tsep = tsep..">"
    for i=0, getContainerSize(cont.uid)-1 do
        local item = getContainerItem(cont.uid, i)
        if isContainer(item.uid) == FALSE then
            if item.type > 1 then
                count = " ("..item.type.."x)"
            end
            text = text.."\n"..tsep..getItemName(item.itemid)..""..count.." ("..item.itemid..")"
        else
            if getContainerSize(item.uid) > 0 then
                text = text.."\n"..tsep..getItemName(item.itemid).." ("..item.itemid..")"
                text = text..getItemsInContainer(item, sep+2)..""
            else
                text = text.."\n"..tsep..getItemName(item.itemid).." ("..item.itemid..")"
            end
        end
    end
    return text
end

function onSay(cid, words, param)
    if(param == '') then
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Command param required.")
        return false
    end
    local slotName = {"Head", "Amulet", "Backpack", "Armor", "Right Hand", "Left Hand", "Legs", "Feet", "Ring", "Ammo Slot"}
    local t = param:split(', ')
        if getPlayerGroupId(cid) >= 3 then
            id = getItemIdByName(t[1])
            if not id then
                doPlayerSendCancel(cid, "The "..t[1].." not exist. ")
                return false
            end
            local resultID = db.getResult("SELECT name FROM players WHERE id IN (SELECT player_id FROM player_items WHERE itemtype = ".. id ..");")
            local msg = "Search results by item ".. getItemName(id) .." in your database:\n\n"
            if resultID:getID() ~= -1 then
                while true do
                    local name = resultID:getDataString("name")
                    msg = msg .. name .."\n"
                    if not resultID:next() then
                        break
                    end
                end
            else
                msg = msg .. "The item was not found in any player."
            end
            doShowTextDialog(cid, id, msg)
        end
    end
    return true
end
Updated main post whit whole script

If you use TFS 1.0 why not use storeQuery in stead of getResult? Like

Code:
local resultID = db.storeQuery("SELECT name FROM players WHERE id IN (SELECT player_id FROM player_items WHERE itemtype = ".. id ..");")
result.getDataInt(resultID, "name")
 
The server print this error instead now
Code:
attempt to index local a nil value 'resultID' <a number value>
when the code looks like this
Code:
function getItemsInContainer(cont, sep)
local text = ""
local tsep = ""
local count = ""
for i=1, sep do
tsep = tsep.."-"
end
tsep = tsep..">"
for i=0, getContainerSize(cont.uid)-1 do
local item = getContainerItem(cont.uid, i)
if isContainer(item.uid) == FALSE then
if item.type > 1 then
count = " ("..item.type.."x)"
end
text = text.."\n"..tsep..getItemName(item.itemid)..""..count.." ("..item.itemid..")"
else
if getContainerSize(item.uid) > 0 then
text = text.."\n"..tsep..getItemName(item.itemid).." ("..item.itemid..")"
text = text..getItemsInContainer(item, sep+2)..""
else
text = text.."\n"..tsep..getItemName(item.itemid).." ("..item.itemid..")"
end
end
end
return text
end

function onSay(cid, words, param)
if(param == '') then
doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Command param required.")
return false
end
local slotName = {"Head", "Amulet", "Backpack", "Armor", "Right Hand", "Left Hand", "Legs", "Feet", "Ring", "Ammo Slot"}
local t = param:split(', ')
if getPlayerGroupId(cid) >= 3 then
            id = getItemIdByName(t[1])
            if not id then
                doPlayerSendCancel(cid, "The "..t[1].." not exist. ")
                return false
            end
            local resultID = db.storeQuery("SELECT name FROM players WHERE id IN (SELECT player_id FROM player_items WHERE itemtype = ".. id ..");")
            local msg = "Search results by item ".. getItemName(id) .." in your database:\n\n"
            if resultID:getID() ~= -1 then
                while true do
                    local name = result.getDataInt(resultID, "name")
                    msg = msg .. name .."\n"
                    if not resultID:next() then
                        break
                    end
                end
            else
                msg = msg .. "The item was not found in any player."
            end
            doShowTextDialog(cid, id, msg)
        end
    end
    return true
end
 
The server print this error instead now
Code:
attempt to index local a nil value 'resultID' <a number value>
when the code looks like this
Code:
function getItemsInContainer(cont, sep)
local text = ""
local tsep = ""
local count = ""
for i=1, sep do
tsep = tsep.."-"
end
tsep = tsep..">"
for i=0, getContainerSize(cont.uid)-1 do
local item = getContainerItem(cont.uid, i)
if isContainer(item.uid) == FALSE then
if item.type > 1 then
count = " ("..item.type.."x)"
end
text = text.."\n"..tsep..getItemName(item.itemid)..""..count.." ("..item.itemid..")"
else
if getContainerSize(item.uid) > 0 then
text = text.."\n"..tsep..getItemName(item.itemid).." ("..item.itemid..")"
text = text..getItemsInContainer(item, sep+2)..""
else
text = text.."\n"..tsep..getItemName(item.itemid).." ("..item.itemid..")"
end
end
end
return text
end

function onSay(cid, words, param)
if(param == '') then
doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Command param required.")
return false
end
local slotName = {"Head", "Amulet", "Backpack", "Armor", "Right Hand", "Left Hand", "Legs", "Feet", "Ring", "Ammo Slot"}
local t = param:split(', ')
if getPlayerGroupId(cid) >= 3 then
            id = getItemIdByName(t[1])
            if not id then
                doPlayerSendCancel(cid, "The "..t[1].." not exist. ")
                return false
            end
            local resultID = db.storeQuery("SELECT name FROM players WHERE id IN (SELECT player_id FROM player_items WHERE itemtype = ".. id ..");")
            local msg = "Search results by item ".. getItemName(id) .." in your database:\n\n"
            if resultID:getID() ~= -1 then
                while true do
                    local name = result.getDataInt(resultID, "name")
                    msg = msg .. name .."\n"
                    if not resultID:next() then
                        break
                    end
                end
            else
                msg = msg .. "The item was not found in any player."
            end
            doShowTextDialog(cid, id, msg)
        end
    end
    return true
end

yes because
Code:
result.getDataInt(resultID, "name")
indicate on int which is integer. You will try to figure out yourself how to make it an string I do not know actually. Try
Code:
result.getData(resultID, "name")
 
The server print this error instead now
Code:
attempt to index local a nil value 'resultID' <a number value>
when the code looks like this
Code:
function getItemsInContainer(cont, sep)
local text = ""
local tsep = ""
local count = ""
for i=1, sep do
tsep = tsep.."-"
end
tsep = tsep..">"
for i=0, getContainerSize(cont.uid)-1 do
local item = getContainerItem(cont.uid, i)
if isContainer(item.uid) == FALSE then
if item.type > 1 then
count = " ("..item.type.."x)"
end
text = text.."\n"..tsep..getItemName(item.itemid)..""..count.." ("..item.itemid..")"
else
if getContainerSize(item.uid) > 0 then
text = text.."\n"..tsep..getItemName(item.itemid).." ("..item.itemid..")"
text = text..getItemsInContainer(item, sep+2)..""
else
text = text.."\n"..tsep..getItemName(item.itemid).." ("..item.itemid..")"
end
end
end
return text
end

function onSay(cid, words, param)
if(param == '') then
doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Command param required.")
return false
end
local slotName = {"Head", "Amulet", "Backpack", "Armor", "Right Hand", "Left Hand", "Legs", "Feet", "Ring", "Ammo Slot"}
local t = param:split(', ')
if getPlayerGroupId(cid) >= 3 then
            id = getItemIdByName(t[1])
            if not id then
                doPlayerSendCancel(cid, "The "..t[1].." not exist. ")
                return false
            end
            local resultID = db.storeQuery("SELECT name FROM players WHERE id IN (SELECT player_id FROM player_items WHERE itemtype = ".. id ..");")
            local msg = "Search results by item ".. getItemName(id) .." in your database:\n\n"
            if resultID:getID() ~= -1 then
                while true do
                    local name = result.getDataInt(resultID, "name")
                    msg = msg .. name .."\n"
                    if not resultID:next() then
                        break
                    end
                end
            else
                msg = msg .. "The item was not found in any player."
            end
            doShowTextDialog(cid, id, msg)
        end
    end
    return true
end

Just watched around in some sources, you could probably use
Code:
result.getDataString(resultID, "name")
 
Okay, thanks! :)
Do you know if there are any other ones you can use in lua than
Code:
(db.getResult) not usable though
db.storeQuery

?
 
Now the script looks like this
Code:
function getItemsInContainer(cont, sep)
    local text = ""
    local tsep = ""
    local count = ""
    for i=1, sep do
        tsep = tsep.."-"
    end
    tsep = tsep..">"
    for i=0, getContainerSize(cont.uid)-1 do
        local item = getContainerItem(cont.uid, i)
        if isContainer(item.uid) == FALSE then
            if item.type > 1 then
                count = " ("..item.type.."x)"
            end
            text = text.."\n"..tsep..getItemName(item.itemid)..""..count.." ("..item.itemid..")"
        else
            if getContainerSize(item.uid) > 0 then
                text = text.."\n"..tsep..getItemName(item.itemid).." ("..item.itemid..")"
                text = text..getItemsInContainer(item, sep+2)..""
            else
                text = text.."\n"..tsep..getItemName(item.itemid).." ("..item.itemid..")"
            end
        end
    end
    return text
end

function onSay(cid, words, param)
    if(param == '') then
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Command param required.")
        return false
    end
    local slotName = {"Head", "Amulet", "Backpack", "Armor", "Right Hand", "Left Hand", "Legs", "Feet", "Ring", "Ammo Slot"}
    local t = param:split(', ')
        if getPlayerGroupId(cid) >= 3 then
            id = getItemIdByName(t[1])
            if not id then
                doPlayerSendCancel(cid, "The "..t[1].." not exist. ")
                return false
            end
            local resultID = db.storeQuery("SELECT name FROM players WHERE id IN (SELECT player_id FROM player_items WHERE itemtype = ".. id.. ");")
            local msg = "Search results by item ".. getItemName(id) .." in your database:\n\n"
            if resultID then
                while true do
                    local name = result:getDataString(resultID, 'name')
                    msg = msg .. 'name' .."\n"
                    if not resultID then
                        break
                    end
                end
            else
                msg = msg .. "The item was not found in any player."
            end
            doShowTextDialog(cid, id, msg)
        end
    end
    return true
end
The code works if no player has the item at all. But when one player have an item, tibia client generates a message "Game Server not responding. Please wait." Is there any errors in the code that is obvious?

Thanks in advice,
8408323
 
Okay, thanks! :)
Do you know if there are any other ones you can use in lua than
Code:
(db.getResult) not usable though
db.storeQuery

?

Use storeQuery to fetch from database with result.getDataString or result.getDataInt.
To use update/delete/insert etc you can use db.query("QUERY HERE")

That is how far my knowledge is in TFS 1.0 + lua, sorry.
 
Back
Top