• 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 help error "round & random"

miguelshta

Member
Joined
Mar 21, 2009
Messages
351
Solutions
1
Reaction score
13
Location
Toronto, Canada
hello i have this 2 errors on my console

Lua:
[Error - GlobalEvent Interface]
[5:33:19.167] data/globalevents/scripts/botmanager.lua:onThink
[5:33:19.167] Description:
[5:33:19.167] data/globalevents/scripts/botmanager.lua:57: bad argument #2 to 'random' (interval is empty)
[5:33:19.183] stack traceback:
[5:33:19.183]   [C]: in function 'random'
[5:33:19.183]   data/globalevents/scripts/botmanager.lua:57: in function <data/globalevents/scripts/botmanager.lua:7>
[5:33:19.199] [Error - GlobalEvents::think] Couldn't execute event: botmanager
this is my botsmanager.lua
Lua:
local keepOnline = 23
local maxChange = 2

local minOnline = 2
local maxOnline = 35

function onThink(interval, lastExecution, thinkInterval)
    if(getStorage(1666) ~= -1) then
        keepOnline = getStorage(1666)
        doSetStorage(1666, -1)
        print("Changed bots number to: " .. keepOnline)
    end
    if(getStorage(1667) ~= -1) then
        minOnline = getStorage(1667)
        doSetStorage(1667, -1)
        print("Changed min bots number to: " .. minOnline)
    end
    if(getStorage(1668) ~= -1) then
        maxOnline = getStorage(1668)
        doSetStorage(1668, -1)
        print("Changed max bots number to: " .. maxOnline)
    end
    if(math.random(1,4) == 2) then
        keepOnline = math.min(maxOnline, math.max(keepOnline+math.random(-1,1), minOnline))
    end
    local online = getPlayersOnline()
    local botsOnline = {}
    local botsOffline = {}
    local playersOnline = {}
    for _, cid in pairs(online) do
        doPlayerFeed(cid, 9999)
        if(isBot(cid) and isBotActive(cid)) then
            table.insert(botsOnline, cid)
        elseif(not isBot(cid)) then
            table.insert(playersOnline, cid)
        else
            table.insert(botsOffline, cid)
        end
    end
    print("BOTS: " .. #botsOnline .. " (" .. keepOnline .. "), P: " .. #playersOnline)
    if(#playersOnline+#botsOnline > keepOnline) then
        if(#botsOnline > 0) then
            local toRemove = math.min(maxChange, #playersOnline+#botsOnline-keepOnline)
            for i = 1, toRemove do
                for i, bot in pairs(botsOnline) do
                    if(getTileInfo(getThingPosition(bot)).protection) then
                        setBotActive(bot, false)
                        table.remove(botsOnline, i)
                        break
                    end
                end
            end
        end
    elseif(#playersOnline+#botsOnline < keepOnline) then
        for i = 1, maxChange do
            if(#playersOnline+#botsOnline+i <= keepOnline) then
                local bot = botsOffline[math.random(1,#botsOffline)]
                setBotActive(bot, true)
            end
        end
    end
    return true
end

function setBotDelayed(name)
    setBot(getPlayerByName(name), 1)
    setBotActive(getPlayerByName(name), false)
end

function onStartup()
    for i=1,200 do
        loadPlayer(getBotNameByID(i))
        addEvent(setBotDelayed, 1000, getBotNameByID(i))
    end
    return true
end

and this is my another error:\

Code:
[Error - CreatureScript Interface]
[5:36:38.383] data/creaturescripts/scripts/war/preparedeath.lua:onPrepareDeath
[5:36:38.383] Description:
[5:36:38.383] data/creaturescripts/scripts/war/preparedeath.lua:12: attempt to call global 'round' (a nil value)
[5:36:38.398] stack traceback:
[5:36:38.398]   data/creaturescripts/scripts/war/preparedeath.lua:12: in function <data/creaturescripts/scripts/war/preparedeath.lua:8>

and my preparedeath.lua

Lua:
function removePzBlock(cid)
    if(isPlayer(cid) and getTileInfo(getThingPosition(cid)).protection) then
        doRemoveConditions(cid)
        doPlayerSetPzLocked(cid, false)
    end
end

function onPrepareDeath(cid, deathList)
    local killerGUID = lastHitOnGuy[getPlayerGUID(cid)]
    local damageDealed = getDamageCounterTable(cid)
    local totalDamage = 1
    local expLost = round((math.floor(getPlayerExperience(cid) - getExperienceForLevel(LEVEL_MINIMUM)) / 5) * 1, 0)
    local attackersNumber = 0
    if(expLost < 250000) then
        expLost = 250000
    end
    local expRealLost = round((expLost / 2) * ((LEVEL_MINIMUM+50) / (getPlayerLevel(cid)+50)), 0)
    for guid, damage in pairs(damageDealed) do
        if(damage > 1) then
            totalDamage = totalDamage + damage
            attackersNumber = attackersNumber + 1
        end
    end
    local killRatio = 1
    if(attackersNumber <= 4) then -- guy + 2 summons
        killRatio = 0.8
    end
    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You lost " .. round(expRealLost, 0) .. " experience points, because of death after attack of " .. attackersNumber .. " players that dealed " .. round(totalDamage, 0) .. " damage.")
    local expGiven = 0
    for guid, damage in pairs(damageDealed) do
        if(damage > 1) then
            local attacker = getPlayerByGUID(guid)
            if(isPlayer(attacker)) then -- if is still online
                local damagePercent = (damage / totalDamage) * 100
                local roundedDamagePercent = round(damagePercent, 2)
                local expRatio, text = getKillExpRatio(attacker, cid)
                expRatio = expRatio * killRatio
                if(damagePercent > math.random(60, 90)) then
                    expRatio = expRatio * 0.8
                end
                local expGained = math.floor(((expLost * damagePercent / 100) * expRatio) / 2)
                --print(getCreatureName(attacker) .. " dostal " .. expGained .. " exp" .. text)
                if(text ~= "" and damagePercent > 0.1) then
                    doPlayerSendTextMessage(attacker, MESSAGE_STATUS_CONSOLE_BLUE, "You received " .. round(expGained, 0) .. " exp (exp-ratio: " .. round(expRatio * 100, 2) .. " %, damage: " .. roundedDamagePercent .. "%) for killing " .. getCreatureName(cid) .. ", because of: " .. text)
                elseif(expGained > 0) then
                    doPlayerSendTextMessage(attacker, MESSAGE_STATUS_CONSOLE_BLUE, "You received " .. round(expGained, 0) .. " exp (exp-ratio: " .. round(expRatio * 100, 2) .. " %, damage: " .. roundedDamagePercent .. " %) for killing " .. getCreatureName(cid) .. ".")
                end
                if(expRatio > 0) then -- frag not blocked
                    war_doPlayerAddExperience(attacker, expGained)
                    expGiven = expGiven + expGained
                    if(expGained > 10000) then
                        addKillToKillsCounter(attacker, cid)
                    end
                    if(guid == killerGUID) then
                        local extraExp = 30000 * expRatio
                        doSendAnimatedText(getThingPosition(attacker), round(expGained+extraExp, 0), COLOR_WHITE)
                        expGiven = expGiven + extraExp
                        war_doPlayerAddExperience(attacker, extraExp)
                        doPlayerAddSoul(attacker, 1)
                        setFrags(attacker, getFrags(attacker)+1)
                        setTmpFrags(attacker, getTmpFrags(attacker)+1)
                        setLastFrags(attacker, getLastFrags(attacker)+1)
                        for _, pid in pairs(getPlayersOnline()) do
                            doPlayerSendChannelMessage(pid, "System", getCreatureName(attacker) .. " (" .. getPlayerLevel(attacker) .. ") killed " .. getCreatureName(cid) .. " (" .. getPlayerLevel(cid) .. ")", 15,5)
                        end
                        doPlayerSendTextMessage(attacker, MESSAGE_STATUS_CONSOLE_BLUE, "You did last hit on " .. getCreatureName(cid) .. ". You receive frag and extra " .. round(extraExp,0) .. " exp.")
                        -- drop body
                        if(getPlayerSex(cid) == 0) then -- female
                            local corpse = doCreateItem(6081, 1, getThingPosition(cid))
                            if(corpse and type(corpse) == "number") then
                                doItemSetAttribute(corpse, "name", "dead body. You recognize " .. getCreatureName(cid) .. ". She was killed by " .. getCreatureName(attacker) .. ".")
                                doDecayItem(corpse)
                            end
                        else -- male
                            local corpse = doCreateItem(6080, 1, getThingPosition(cid))
                            if(corpse and type(corpse) == "number") then
                                doItemSetAttribute(corpse, "name", "dead body. You recognize " .. getCreatureName(cid) .. ". He was killed by " .. getCreatureName(attacker) .. ".")
                                doDecayItem(corpse)
                            end
                        end
                    elseif(expGained > 0) then
                        doSendAnimatedText(getThingPosition(attacker), round(expGained, 0), COLOR_WHITE)
                        if(expGained > 2000) then
                            setAssists(attacker, getAssists(attacker)+1)
                            setTmpAssists(attacker, getTmpAssists(attacker)+1)
                            setLastAssists(attacker, getLastAssists(attacker)+1)
                            doPlayerSendTextMessage(attacker, MESSAGE_STATUS_CONSOLE_BLUE, "You did hit on " .. getCreatureName(cid) .. ". You receive assist.")
                        end
                    end
                end
            end
        end
    end
    -- add to records on www
    print("Died: " .. getCreatureName(cid) .. "(" .. getPlayerLevel(cid) .. ") lost: " .. expRealLost .. "exp (full: " .. expLost .. " ), players gained on him: " .. round(expGiven,0))
    war_doPlayerRemoveExperience(cid, expRealLost)
    setDeaths(cid, getDeaths(cid)+1)
    setTmpDeaths(cid, getTmpDeaths(cid)+1)
    if(lastKiller[killerGUID] ~= nil and lastKiller[killerGUID] == getPlayerGUID(cid)) then
        -- lastKiller[killerGUID] zabil tego kto zabil ostatnio ciebie, zemsta?
    end
    if(lastKiller[getPlayerGUID(cid)] ~= nil and lastKiller[getPlayerGUID(cid)] == killerGUID) then
        -- znowu ten sam zabil co wczesniej, cos zrobic?
    end
    -- save alive time
    war_saveTimeAliveRecord(cid)
    db.executeQuery("UPDATE `players` SET `kills` = " .. getFrags(cid) .. ", `tmp_kills` = " .. getTmpFrags(cid) .. ", `deaths` = " .. getDeaths(cid) .. ", `tmp_deaths` = " .. getTmpDeaths(cid) .. ", `assists` = " .. getAssists(cid) .. ", `tmp_assists` = " .. getTmpAssists(cid) .. " WHERE `id` = " .. getPlayerGUID(cid) .. ";")
    setLastAssists(cid, 0)
    setLastFrags(cid, 0)
    resetTimeAlive(cid)
    resetDamageCounter(cid)
    lastKiller[getPlayerGUID(cid)] = killerGUID
    
    -- return false i lecz gracza co pada i tp do swiatyni
    doSendMagicEffect(getThingPosition(cid), CONST_ME_YALAHARIGHOST)
    if(isBot(cid)) then
        doTeleportThing(cid, getTownTemplePosition(math.random(1, 6)))
    else
        doTeleportThing(cid, getTownTemplePosition(math.random(1, 13)))
    end
    -- HP i MANA fix
    recalculateHPandMana(cid)
    doRemoveConditions(cid)
    doPlayerSetPzLocked(cid, false)
    addEvent(removePzBlock, 1000, cid)
    addEvent(removePzBlock, 5500, cid)
    return true
end
 
Your first error occurs when the 2nd parameter is lower then the first one.

Lua:
local bot = botsOffline[math.random(1,#botsOffline)]
So your array of botsOffline is less then 1.

You'll have to figure out what you want to do to logic your way out of that scenario.

--
Second error, is that you're missing a function.

There's no way to know what the original function did exactly.. but we can guess.

If it's just a simple rounding function to the nearest integer, I'd suggest something like this.
Lua:
function round(number)
    number = math.floor((number + 0.5))
    return number
end
 
Your first error occurs when the 2nd parameter is lower then the first one.

Lua:
local bot = botsOffline[math.random(1,#botsOffline)]
So your array of botsOffline is less then 1.

You'll have to figure out what you want to do to logic your way out of that scenario.

--
Second error, is that you're missing a function.

There's no way to know what the original function did exactly.. but we can guess.

If it's just a simple rounding function to the nearest integer, I'd suggest something like this.
Lua:
function round(number)
    number = math.floor((number + 0.5))
    return number
end
what do you recommend to do? change my minimum to 0?
 
what do you recommend to do? change my minimum to 0?

Idk how your offline bots could be 0, because I have no idea how the offline bots work.

This solution will solve the error, but won't fix the issue of 0 bots.
Lua:
for i = 1, maxChange do
    if(#playersOnline+#botsOnline+i <= keepOnline) then
        if #botsOffline > 0 then
            local bot = botsOffline[math.random(1,#botsOffline)]
            setBotActive(bot, true)
        end
    end
end
 
Back
Top