• 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 Check highest level online.

DukeeH

Active Member
Joined
Dec 6, 2010
Messages
550
Solutions
3
Reaction score
39
Hello, I'm trying to rewrite some old scripts and I need help as 1.x (1.3 on my case) doesn't have online column on table players.
This is the code I used to have:
Lua:
function onThink(interval)
local query = db.getResult("Select * FROM `players` WHERE `level` > 350 and `online`=1 ORDER BY `level` DESC;")
if query:getID() ~= -1 then
   local name = query:getDataString("name")
   if isPlayerOnline(name) then
   end
end
return TRUE
end

function isPlayerOnline(name)
players=getPlayersOnline()
for _,pid in ipairs(players) do
    if getPlayerName(pid):lower() == name:lower() then
       return true
    end
end
return false
end
Tried a few changes like:
db.getResult to db.storeQuery...
But I have no idea a good way to get the same result on 1.x
Thanks guys.
 
Solution
Why would you even loop the database for that? Just check the highest level in the getPlayersOnline table that's enough.

Lua:
local highest = {level = 0, cid = 0}

for _, cid in ipairs(getPlayersOnline()) do
    if getPlayerLevel(cid) > highest.level then
       highest.level = getPlayerLevel(cid)
       highest.cid = cid
    end
end

-- here you can use highest.cid and highest.level.

If this code will run often and you care about performance you could use an onLogin/onLogout script to store player info in a variable. That way, whenever you want to fetch the highest level player you don't need to loop through online players each time.
Why would you even loop the database for that? Just check the highest level in the getPlayersOnline table that's enough.

Lua:
local highest = {level = 0, cid = 0}

for _, cid in ipairs(getPlayersOnline()) do
    if getPlayerLevel(cid) > highest.level then
       highest.level = getPlayerLevel(cid)
       highest.cid = cid
    end
end

-- here you can use highest.cid and highest.level.

If this code will run often and you care about performance you could use an onLogin/onLogout script to store player info in a variable. That way, whenever you want to fetch the highest level player you don't need to loop through online players each time.
 
Last edited:
Solution
Why would you even loop the database for that? Just check the highest level in the getPlayersOnline table that's enough.

Lua:
local highest = {level = 0, cid = 0}

for _, cid in ipairs(getPlayersOnline()) do
    if getPlayerLevel(cid) > highestLevel then
       highest.level = getPlayerLevel(cid)
       highest.cid = cid
    end
end

-- here you can use highest.cid and highest.level.

If this code will run often and you care about performance you could use an onLogin/onLogout script to store player info in a variable. That way, whenever you want to fetch the highest level player you don't need to loop through online players each time.
Thanks @Colandus.
It was an old script, I thought there was a better (faster) way to do it, will try to rewrite with your code.
I don't think there's a need for onlogin, logout, it would run on a time like 10mins. Would getPlayersOnline checking all players be heavy?
Thanks once again!
 
No, once every 10 min is fine. If it runs each second or each 2 seconds, that would be unnecessary workload (although not so heavy, but still unnecessary).
 
No, once every 10 min is fine. If it runs each second or each 2 seconds, that would be unnecessary workload (although not so heavy, but still unnecessary).
8d4bad8f31b241c1875367cdeb065eed.png

Lua:
for _, cid in ipairs(getPlayersOnline()) do
    if getPlayerLevel(cid) > highestLevel then
       highest.level = getPlayerLevel(cid)
       highest.cid = cid
    end
end

Is it right for 1.3?
Could you please check? Tried to fix, but couldn't find the getplayersonline and what it needs.
 
Sorry, I didn't see you said you wanted it for 1.3.

Lua:
local highest = {level = 0}

for _, player in ipairs(Game.getPlayers()) do
    if player:getLevel() > highest.level then
       highest.level = player:getLevel()
       highest.player = player
    end
end

if highest.player then
    -- use highest.player here (userdata)
end


Or another option would be to use this function I just created will be easier and somewhat prettier (but slightly less efficient, but shouldn't matter if you're not running it very often):
Lua:
function table.filter(t, filterFunction)
    local match
    for _, val in pairs(t) do
        if not match or filterFunction(val, match) then
            match = val
        end
    end
    return match
end

Then your code would look like:
Lua:
local player = table.filter(Game.getPlayers(), function(a, b) return a:getLevel() > b:getLevel() end)

-- we only need this check in case there are no players online. then player would be nil
if player then
    --- your code here
end
 
Last edited:
Back
Top