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

globalevents onstartup

Dran Ryszard

Active Member
Joined
Apr 25, 2023
Messages
139
Reaction score
30
Location
Poland
Hi!
I added a "function" what loading map in X postions from other file and i want to load "random" part of map, from random file when server starting.
And it work, but i was add that code to onStartUp.lua, but i don't know why that file is loading two times, then on start that script load two parts of map not one, any ideas?


1736627652386.webp
1736627670373.webp
 
Solution
but i don't know why that file is loading two times,
Lua is loaded 2 times and problem is that your code is outside onStartup event.
It should be:
LUA:
function onStartup()

    db.query("TRUNCATE TABLE `players_online`")
    db.asyncQuery("DELETE FROM `guild_wars` WHERE `status` = 0")
    db.asyncQuery("DELETE FROM `players` WHERE `deletion` != 0 AND `deletion` < " .. os.time())
    db.asyncQuery("DELETE FROM `ip_bans` WHERE `expires_at` != 0 AND `expires_at` <= " .. os.time())

    -- Move expired bans to ban history
    local resultId = db.storeQuery("SELECT * FROM `account_bans` WHERE `expires_at` != 0 AND `expires_at` <= " .. os.time())
    if resultId ~= false then
        repeat
            local accountId =...
Can you post your onStartUp.lua file?

LUA:
function onStartup()

    db.query("TRUNCATE TABLE `players_online`")
    db.asyncQuery("DELETE FROM `guild_wars` WHERE `status` = 0")
    db.asyncQuery("DELETE FROM `players` WHERE `deletion` != 0 AND `deletion` < " .. os.time())
    db.asyncQuery("DELETE FROM `ip_bans` WHERE `expires_at` != 0 AND `expires_at` <= " .. os.time())

    -- Move expired bans to ban history
    local resultId = db.storeQuery("SELECT * FROM `account_bans` WHERE `expires_at` != 0 AND `expires_at` <= " .. os.time())
    if resultId ~= false then
        repeat
            local accountId = result.getNumber(resultId, "account_id")
            db.asyncQuery("INSERT INTO `account_ban_history` (`account_id`, `reason`, `banned_at`, `expired_at`, `banned_by`) VALUES (" .. accountId .. ", " .. db.escapeString(result.getString(resultId, "reason")) .. ", " .. result.getNumber(resultId, "banned_at") .. ", " .. result.getNumber(resultId, "expires_at") .. ", " .. result.getNumber(resultId, "banned_by") .. ")")
            db.asyncQuery("DELETE FROM `account_bans` WHERE `account_id` = " .. accountId)
        until not result.next(resultId)
        result.free(resultId)
    end

    -- Check house auctions
    local resultId = db.storeQuery("SELECT `id`, `highest_bidder`, `last_bid`, (SELECT `balance` FROM `players` WHERE `players`.`id` = `highest_bidder`) AS `balance` FROM `houses` WHERE `owner` = 0 AND `bid_end` != 0 AND `bid_end` < " .. os.time())
    if resultId ~= false then
        repeat
            local house = House(result.getNumber(resultId, "id"))
            if house then
                local highestBidder = result.getNumber(resultId, "highest_bidder")
                local balance = result.getNumber(resultId, "balance")
                local lastBid = result.getNumber(resultId, "last_bid")
                if balance >= lastBid then
                    db.query("UPDATE `players` SET `balance` = " .. (balance - lastBid) .. " WHERE `id` = " .. highestBidder)
                    house:setOwnerGuid(highestBidder)
                end
                db.asyncQuery("UPDATE `houses` SET `last_bid` = 0, `bid_end` = 0, `highest_bidder` = 0, `bid` = 0 WHERE `id` = " .. house:getId())
            end
        until not result.next(resultId)
        result.free(resultId)
    end

    -- store towns in database
    db.query("TRUNCATE TABLE `towns`")
    for i, town in ipairs(Game.getTowns()) do
        local position = town:getTemplePosition()
        db.query("INSERT INTO `towns` (`id`, `name`, `posx`, `posy`, `posz`) VALUES (" .. town:getId() .. ", " .. db.escapeString(town:getName()) .. ", " .. position.x .. ", " .. position.y .. ", " .. position.z .. ")")
    end
end
local maps_instance = {
[1] = "lava",
[2] = "earth",
[3] = "winter"
}
local maps_get = math.random(1,3)
print("Loaded part map: " .. maps_instance[maps_get])
Game.loadMapChunk("data/world/insty/"..maps_instance[maps_get]..".otbm", Position(5000, 5000, 0))
 
but i don't know why that file is loading two times,
Lua is loaded 2 times and problem is that your code is outside onStartup event.
It should be:
LUA:
function onStartup()

    db.query("TRUNCATE TABLE `players_online`")
    db.asyncQuery("DELETE FROM `guild_wars` WHERE `status` = 0")
    db.asyncQuery("DELETE FROM `players` WHERE `deletion` != 0 AND `deletion` < " .. os.time())
    db.asyncQuery("DELETE FROM `ip_bans` WHERE `expires_at` != 0 AND `expires_at` <= " .. os.time())

    -- Move expired bans to ban history
    local resultId = db.storeQuery("SELECT * FROM `account_bans` WHERE `expires_at` != 0 AND `expires_at` <= " .. os.time())
    if resultId ~= false then
        repeat
            local accountId = result.getNumber(resultId, "account_id")
            db.asyncQuery("INSERT INTO `account_ban_history` (`account_id`, `reason`, `banned_at`, `expired_at`, `banned_by`) VALUES (" .. accountId .. ", " .. db.escapeString(result.getString(resultId, "reason")) .. ", " .. result.getNumber(resultId, "banned_at") .. ", " .. result.getNumber(resultId, "expires_at") .. ", " .. result.getNumber(resultId, "banned_by") .. ")")
            db.asyncQuery("DELETE FROM `account_bans` WHERE `account_id` = " .. accountId)
        until not result.next(resultId)
        result.free(resultId)
    end

    -- Check house auctions
    local resultId = db.storeQuery("SELECT `id`, `highest_bidder`, `last_bid`, (SELECT `balance` FROM `players` WHERE `players`.`id` = `highest_bidder`) AS `balance` FROM `houses` WHERE `owner` = 0 AND `bid_end` != 0 AND `bid_end` < " .. os.time())
    if resultId ~= false then
        repeat
            local house = House(result.getNumber(resultId, "id"))
            if house then
                local highestBidder = result.getNumber(resultId, "highest_bidder")
                local balance = result.getNumber(resultId, "balance")
                local lastBid = result.getNumber(resultId, "last_bid")
                if balance >= lastBid then
                    db.query("UPDATE `players` SET `balance` = " .. (balance - lastBid) .. " WHERE `id` = " .. highestBidder)
                    house:setOwnerGuid(highestBidder)
                end
                db.asyncQuery("UPDATE `houses` SET `last_bid` = 0, `bid_end` = 0, `highest_bidder` = 0, `bid` = 0 WHERE `id` = " .. house:getId())
            end
        until not result.next(resultId)
        result.free(resultId)
    end

    -- store towns in database
    db.query("TRUNCATE TABLE `towns`")
    for i, town in ipairs(Game.getTowns()) do
        local position = town:getTemplePosition()
        db.query("INSERT INTO `towns` (`id`, `name`, `posx`, `posy`, `posz`) VALUES (" .. town:getId() .. ", " .. db.escapeString(town:getName()) .. ", " .. position.x .. ", " .. position.y .. ", " .. position.z .. ")")
    end
    local maps_instance = {
        [1] = "lava",
        [2] = "earth",
        [3] = "winter"
    }
    local maps_get = math.random(1,3)
    print("Loaded part map: " .. maps_instance[maps_get])
    Game.loadMapChunk("data/world/insty/"..maps_instance[maps_get]..".otbm", Position(5000, 5000, 0))
end
Then no matter how many times OTS process Lua files, it will be called just once, when all onStartup events execute.
 
Solution
Back
Top