• 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 get length of local 'players' (a boolean value)

Wezza

lua nOOb
Joined
May 31, 2008
Messages
2,278
Reaction score
31
Code:
./data/lib/cs_functions.lua:492: attempt to get length of local 'players' (a boolean value)
 stack traceback:
 ./data/lib/cs_functions.lua:492: in function <./data/lib/cs_functions.lua:490>

Function:

Code:
function closeCs()
    local players = getCsPlayers()
    print(#players)
    doBroadcastMessage("Counter-Strike is over! Total score: Terror: " .. getGlobalStorageValue(887786) .. ", Counter: " .. getGlobalStorageValue(887787) .. ". Next Counter-Strike: 22.00 CET tomorrow.", 22)
    setGlobalStorageValue(888888, 0)
    for _, cid in ipairs(players) do
        leaveCs(cid)
    end
end
 
getCsPlayers() is the problem, it returns a boolean when you're expecting a table. Post that function as well.
 
Code:
function getCsPlayers()
    local result = db.getResult("SELECT `player_id` FROM `cs`")

    if result:getID() ~= LUA_ERROR then
        local players = {}
        repeat
            local player_id = result:getDataInt "player_id"
            if player_id ~= nil then
                local cid = getPlayerByName(getPlayerNameByGUID(player_id))
                if isPlayer(cid) == TRUE then
                    table.insert(players, cid)
                else
                    db.executeQuery("DELETE FROM `cs` WHERE `player_id` = " .. player_id .. ";")
                end
            end
        until not result:next()
        
        result:free()
        return players
    end   
    return LUA_ERROR
end

@Stigma
 
You should always return the same type from your functions. My suggestion is that if db.getResult() returns LUA_ERROR, then you should log that error:
Lua:
function getCsPlayers()
    local players = {}
    local result = db.getResult("SELECT `player_id` FROM `cs`")
    if result:getID() == LUA_ERROR then
        -- log error here
        return players
    end

    repeat
        local player_id = result:getDataInt "player_id"
        if player_id ~= nil then
            local cid = getPlayerByName(getPlayerNameByGUID(player_id))
            if isPlayer(cid) == TRUE then
                table.insert(players, cid)
            else
                db.executeQuery("DELETE FROM `cs` WHERE `player_id` = " .. player_id .. ";")
            end
        end
    until not result:next()
 
    result:free()
    return players
end
 
Back
Top