• 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 script that display the top level of online players

5lave Ots

Active Member
Joined
Oct 2, 2017
Messages
246
Solutions
1
Reaction score
40
Location
Ankrahmun
hey outlanders
i request an LUA script that display the highest online player as it CID
to be used down for an effect
using 0,4 tibia 860
thanks
 
Solution
You could use a function like this to accomplish that, if your server doesn't have too many players:

Lua:
-- Returns the 'cid' of the highest level online player.
function getHighestLevelOnlinePlayer()
    local highest = {
        level = 0,
        player = 0
    }
    
        for _, name in ipairs(getOnlinePlayers()) do -- Loop through all online players
                local cid = getPlayerByName(name)
                if isPlayer(cid) then
                    if getPlayerLevel(cid) > highest.level then
                        highest.level = getPlayerLevel(cid)
                        highest.player = cid
                    end
                end
        end
        
        print("Highest level online player was: " ...
You could use a function like this to accomplish that, if your server doesn't have too many players:

Lua:
-- Returns the 'cid' of the highest level online player.
function getHighestLevelOnlinePlayer()
    local highest = {
        level = 0,
        player = 0
    }
    
        for _, name in ipairs(getOnlinePlayers()) do -- Loop through all online players
                local cid = getPlayerByName(name)
                if isPlayer(cid) then
                    if getPlayerLevel(cid) > highest.level then
                        highest.level = getPlayerLevel(cid)
                        highest.player = cid
                    end
                end
        end
        
        print("Highest level online player was: " .. getCreatureName(highest.player) .. " with level " .. highest.level .. ".")
        return highest.player
end

However, such "loop through all players" functions can hinder performance if you are looping through too many players, doing too many things within the loop. This one is simple so it shouldn't affect you much, but take caution.

Better way to write it would probably be a Mysql query which simply returns the player which meets the "Online" flag, as well as has the highest "Level".
 
Solution
thanks but how to make it with MySQL, and also Iwill add a level min, so it start loop when player level > 300

You could try something like this - but I don't guarantee this code will work, I am pretty shit with MySQL queries 😥

Lua:
-- Returns the 'cid' of the highest level online player via query.
function getHighestLevelOnlinePlayer()
     local query = db.getResult("SELECT `id` FROM `players` WHERE `online` = 1 ORDER BY `level` DESC LIMIT 1;")
     local guid = query:getDataInt("id")
   
    if guid > 0 then
        return getCreatureByName(getPlayerNameByGUID(guid))
    else
        return 0
    end
end
 
I got an error when no one online, any function to check if online > x?
I tried getPlayersOnline() but tfs cant compare table with numbers
 
Why would you want to get highest online player through sql? You can already check online players, sql would only be needed if the player you are searching for is offline.
 
yes I thought this, so I used the first code to check them from online players,
but I got the above error when no one online? any solution or improve for this?
 
Lua:
-- Returns the 'cid' of the highest level online player.
function getHighestLevelOnlinePlayer()
    local highest = {
        level = 0,
        player = 0
    }
    
    if getOnlinePlayers() >= 1 then
        for _, name in ipairs(getOnlinePlayers()) do -- Loop through all online players
                local cid = getPlayerByName(name)
                if isPlayer(cid) then
                    if getPlayerLevel(cid) > highest.level then
                        highest.level = getPlayerLevel(cid)
                        highest.player = cid
                    end
                end
        end
     end
        
        print("Highest level online player was: " .. getCreatureName(highest.player) .. " with level " .. highest.level .. ".")
        return highest.player
end

this should fix the issue i guess
 
no bro I did that but if getOnlinePlayers() >= 1 then
error: attempt to compare number with table for this line
Lua:
-- Returns the 'cid' of the highest level online player.
function getHighestLevelOnlinePlayer()
    local highest = {
        level = 0,
        player = 0
    }
    
    local online_players = getOnlinePlayers()
    if online_players and #online_players > 0 then
        for _, name in ipairs(online_players) do -- Loop through all online players
            local cid = getPlayerByName(name)
            if isPlayer(cid) and getPlayerLevel(cid) > highest.level then
                highest.level = getPlayerLevel(cid)
                highest.player = cid
            end
        end
    end
        
    print("Highest level online player was: " .. getCreatureName(highest.player) .. " with level " .. highest.level .. ".")
    return highest.player
end
 
Why would you want to get highest online player through sql? You can already check online players, sql would only be needed if the player you are searching for is offline.

It can be a problem depending on the contents of the loop, the amount of people online, and the hardware on the server.
I know because I had a bunch of "loop through all online players" scripts that bombed the server's performance.
SQL performs better in that regard.

But yeah, as I noted in the first post, this one should probably be okay.
 
Back
Top