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

DB query - only one result instead three

sharinn

Active Member
Joined
Aug 27, 2011
Messages
158
Solutions
8
Reaction score
43
GitHub
ArturKnopik
Twitch
krecikondexin
Hi, I want to make a script to create 3 monsters that will pretend to be a top player, the problem is that the query returns only 1 result instead of 3, executing the quesra directly in the DB returns 3 results as I wanted
Can anyone tell what i am doing wrong?
Lua:
local playerPos = {
    [1] = {
        pos = {
            x = 3391,
            y = 2067,
            z = 7
        }
    },
    [2] = {
        pos = {
            x = 3388,
            y = 2067,
            z = 7
        }
    },
    [3] = {
        pos = {
            x = 3394,
            y = 2067,
            z = 7
        }
    }
}

x = playerPos[1].pos

local startup = GlobalEvent("TopPlayer_startup")
function startup.onStartup()
    local index = 1
    local resultId = db.storeQuery(
        "SELECT `name`, `level`, `lookhead`, `lookbody`, `looklegs`, `lookfeet`, `looktype` FROM `players` ORDER BY `level` DESC, `experience` DESC LIMIT 3")

    if resultId then
        repeat
            local name = result.getString(resultId, "name")
            local level = result.getNumber(resultId, "level")
            local lookhead = result.getNumber(resultId, "lookhead")
            local lookbody = result.getNumber(resultId, "lookbody")
            local looklegs = result.getNumber(resultId, "looklegs")
            local lookfeet = result.getNumber(resultId, "lookfeet")
            local looktype = result.getNumber(resultId, "looktype")

            local monsterName = "Top" .. index
            local monster = Game.createMonster(monsterName, playerPos[index].pos)
            if monster then
                print(name)
                monster:rename(name, name)
                local conditionOutfit = Condition(CONDITION_OUTFIT)
                conditionOutfit:setTicks(-1)
                conditionOutfit:setOutfit({
                    lookType = looktype,
                    lookHead = lookhead,
                    lookBody = lookbody,
                    lookLegs = looklegs,
                    lookFeet = lookfeet
                })
                monster:addCondition(conditionOutfit)
            end
            index = index + 1
        until not result.next(resultId)
        result.free(resultId)
    end
end

startup:register()
 
Solution
fixed:

Lua:
local function generateTopPlayers()
    local playerPos = {
        [1] = {
            pos = {
                x = 3391,
                y = 2067,
                z = 7
            },
        },
        [2] = {
            pos = {
                x = 3388,
                y = 2067,
                z = 7
            },
        },
        [3] = {
            pos = {
                x = 3394,
                y = 2067,
                z = 7
            },
        }
    }

    for i, v in ipairs(playerPos) do
        local tile = Tile(v.pos)
        if tile then
            creature = tile:getTopCreature()
            if creature then
                creature:remove()
            end
        end
    end

    local players = {}...
fixed:

Lua:
local function generateTopPlayers()
    local playerPos = {
        [1] = {
            pos = {
                x = 3391,
                y = 2067,
                z = 7
            },
        },
        [2] = {
            pos = {
                x = 3388,
                y = 2067,
                z = 7
            },
        },
        [3] = {
            pos = {
                x = 3394,
                y = 2067,
                z = 7
            },
        }
    }

    for i, v in ipairs(playerPos) do
        local tile = Tile(v.pos)
        if tile then
            creature = tile:getTopCreature()
            if creature then
                creature:remove()
            end
        end
    end

    local players = {}

    local resultId = db.storeQuery(
        "SELECT `level`, `name`, `lookhead`, `lookbody`, `looklegs`, `lookfeet`, `looktype`, `lookaddons` FROM `players` ORDER BY `level` DESC, `experience` DESC LIMIT 3")

    if resultId then
        repeat
            local name = result.getString(resultId, "name")
            local level = result.getNumber(resultId, "level")
            local lookhead = result.getNumber(resultId, "lookhead")
            local lookbody = result.getNumber(resultId, "lookbody")
            local looklegs = result.getNumber(resultId, "looklegs")
            local lookfeet = result.getNumber(resultId, "lookfeet")
            local looktype = result.getNumber(resultId, "looktype")
            local lookaddons = result.getNumber(resultId, "lookaddons")

            table.insert(players, {
                name = name,
                level = level,
                outfit = {
                    lookType = looktype,
                    lookHead = lookhead,
                    lookBody = lookbody,
                    lookLegs = looklegs,
                    lookFeet = lookfeet,
                    lookAddons = lookaddons
                }
            })
        until not result.next(resultId)
        result.free(resultId)

        for i = 1, #players, 1 do
            local monsterName = "Top" .. i
            local monster = Game.createMonster(monsterName, playerPos[i].pos, true, true, 214)
            if monster then
                 monster:rename(i .. ". LVL: ".. players[i].level .."\n" .. players[i].name, players[i].name)
                local conditionOutfit = Condition(CONDITION_OUTFIT)
                conditionOutfit:setTicks(-1)
                conditionOutfit:setOutfit(players[i].outfit)
                   monster:addCondition(conditionOutfit)
            end
        end
    end
end

local startup = GlobalEvent("TopPlayer_startup")
function startup.onStartup()
    generateTopPlayers()
end
 
Solution
Back
Top