• 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 index local '' (a number value) automatic castle system

elementrix

New Member
Joined
Feb 28, 2017
Messages
20
Reaction score
0
Error
Code:
[02/03/2017 02:33:46] [Error - Action Interface] 
[02/03/2017 02:33:46] data/actions/scripts/castle.lua:onUse
[02/03/2017 02:33:46] Description: 
[02/03/2017 02:33:46] data/actions/scripts/castle.lua:11: attempt to index local 'castles' (a number value)
[02/03/2017 02:33:46] stack traceback:
[02/03/2017 02:33:46]     data/actions/scripts/castle.lua:11: in function <data/actions/scripts/castle.lua:3>

Script
Lua:
local lvl = getConfigInfo('levelToBuyHouse')

function onUse(cid, item, fromPosition, itemEx, toPosition)
    if getPlayerLevel(cid) < lvl then
        return doPlayerSendCancel(cid, 'You have to be at least Level ' .. lvl .. ' to purchase a house.')
    elseif isPlayerPzLocked(cid) then
        return doPlayerSendCancel(cid, 'You cannot use this while PZlocked.')
    end

    local castles = db.storeQuery('SELECT id FROM houses WHERE town=4')
    if castles:getID() == -1 then
        return true
    end

    local owners, id, castle = {}, getPlayerGUID(cid), nil
    repeat
        local hid = castles:getDataInt('id')
        local owner = getHouseInfo(hid).owner
        if owner == id then
            return doPlayerSendCancel(cid, 'Your character already owns a castle.')
        elseif owner == 0 or owner == 2 then
            if castle == nil then
                castle = hid
            end
        else
            table.insert(owners, owner)
        end
    until not castles:next()
    castles:free()

    if not castle then
        return doPlayerSendCancel(cid, 'There are no castles available at this moment.')
    end

    local acc = db.query('SELECT id FROM players WHERE account_id = '.. getPlayerAccountId(cid) .. ' AND id != ' .. id)
    if acc:getID() ~= -1 then
        repeat
            if table.find(owners, acc:getDataInt('id')) then
                return doPlayerSendCancel(cid, 'Your account already owns a castle.')
            end
        until not acc:next()
        acc:free()
    end

    doRemoveItem(item.uid)

    local k = getHouseInfo(castle)
    setHouseOwner(castle, id)
    doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, 'Welcome to your new home, ' .. k.name .. '! You can now step into the teleport. To get here again, enter the teleport in Temple +1')
    doBroadcastMessage(getCreatureName(cid) .. ' has bought ' .. k.name .. ' using a Castle Box.', MESSAGE_EVENT_ADVANCE)

    local old = getThingPos(cid)
    doTeleportThing(cid, k.entry)
    doSendMagicEffect(old, CONST_ME_TELEPORT)
    doSendMagicEffect(k.entry, CONST_ME_TELEPORT)
    return true
end
 
Solution
here
Lua:
function onThink()
    local castles = db.storeQuery('SELECT id FROM houses WHERE town=4')
    if not castles then
        return true
    end

    local p = getPlayersOnline()[1]
    repeat
        local hid = result.getDataInt(castles, 'id')
        local h = getHouseInfo(hid)
        if h.owner == 0 then
            setHouseOwner(hid, 2)
        elseif p then
            local n = tonumber(h.name:sub(-2)) or 0
            h.entry.x = h.entry.x + (n % 2 == 0 and 3 or -3)
            doCreatureSay(p, getPlayerNameByGUID(h.owner), TALKTYPE_ORANGE_1, false, 0, h.entry)
        end
    until not result.next(castles)
    result.free(castles)

    return true
end
change
Lua:
if castles:getID() == -1 then
to
Lua:
if not castles then
Kinda same-ish error now

Code:
[02/03/2017 16:08:28] [Error - Action Interface]
[02/03/2017 16:08:28] data/actions/scripts/castle.lua:onUse
[02/03/2017 16:08:28] Description:
[02/03/2017 16:08:28] data/actions/scripts/castle.lua:17: attempt to index local 'castles' (a number value)
[02/03/2017 16:08:28] stack traceback:
[02/03/2017 16:08:28]     data/actions/scripts/castle.lua:17: in function <data/actions/scripts/castle.lua:3>

Same error with global events :V
Code:
[02/03/2017 16:16:07] [Error - GlobalEvent Interface] 
[02/03/2017 16:16:07] data/globalevents/scripts/castles.lua:onThink
[02/03/2017 16:16:08] Description: 
[02/03/2017 16:16:08] data/globalevents/scripts/castles.lua:9: attempt to index local 'castles' (a number value)
[02/03/2017 16:16:08] stack traceback:
[02/03/2017 16:16:08]     data/globalevents/scripts/castles.lua:9: in function <data/globalevents/scripts/castles.lua:1>
[02/03/2017 16:16:08] [Error - GlobalEvents::think] Couldn't execute event: castles

Lua:
function onThink()
    local castles = db.storeQuery('SELECT id FROM houses WHERE town=4')
    if not castles then
        return true
    end

    local p = getPlayersOnline()[1]
    repeat
        local hid = castles:getDataInt('id')
        local h = getHouseInfo(hid)
        if h.owner == 0 then
            setHouseOwner(hid, 2)
        elseif p then
            local n = tonumber(h.name:sub(-2)) or 0
            h.entry.x = h.entry.x + (n % 2 == 0 and 3 or -3)
            doCreatureSay(p, getPlayerNameByGUID(h.owner), TALKTYPE_ORANGE_1, false, 0, h.entry)
        end
    until not castles:next()
    castles:free()

    return true
end
 
Last edited:
castles == nil
Still same error
Code:
[02/03/2017 16:38:35] [Error - Action Interface] 
[02/03/2017 16:38:35] data/actions/scripts/castle.lua:onUse
[02/03/2017 16:38:35] Description: 
[02/03/2017 16:38:35] data/actions/scripts/castle.lua:17: attempt to index local 'castles' (a number value)
[02/03/2017 16:38:35] stack traceback:
[02/03/2017 16:38:35]     data/actions/scripts/castle.lua:17: in function <data/actions/scripts/castle.lua:3>
 
Last edited:
here
Lua:
function onThink()
    local castles = db.storeQuery('SELECT id FROM houses WHERE town=4')
    if not castles then
        return true
    end

    local p = getPlayersOnline()[1]
    repeat
        local hid = result.getDataInt(castles, 'id')
        local h = getHouseInfo(hid)
        if h.owner == 0 then
            setHouseOwner(hid, 2)
        elseif p then
            local n = tonumber(h.name:sub(-2)) or 0
            h.entry.x = h.entry.x + (n % 2 == 0 and 3 or -3)
            doCreatureSay(p, getPlayerNameByGUID(h.owner), TALKTYPE_ORANGE_1, false, 0, h.entry)
        end
    until not result.next(castles)
    result.free(castles)

    return true
end
 
Solution
here
Lua:
function onThink()
    local castles = db.storeQuery('SELECT id FROM houses WHERE town=4')
    if not castles then
        return true
    end

    local p = getPlayersOnline()[1]
    repeat
        local hid = result.getDataInt(castles, 'id')
        local h = getHouseInfo(hid)
        if h.owner == 0 then
            setHouseOwner(hid, 2)
        elseif p then
            local n = tonumber(h.name:sub(-2)) or 0
            h.entry.x = h.entry.x + (n % 2 == 0 and 3 or -3)
            doCreatureSay(p, getPlayerNameByGUID(h.owner), TALKTYPE_ORANGE_1, false, 0, h.entry)
        end
    until not result.next(castles)
    result.free(castles)

    return true
end

Heyy it worked! Thank you so much, could U help with action script? It has the same error
 
Back
Top