• 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 Table orzanige by Higest Value and Names

roriscrave

Advanced OT User
Joined
Dec 7, 2011
Messages
1,188
Solutions
34
Reaction score
200
I have 2 tables, a value table and a string, is possible to make it?
the table can only contain 3 names, and the storages will be organized from largest to smallest
exemple my table are:

Lua:
local table = {1000, 880, 720}
local pNames = {Smavel, Quartz, Newbie}

if a player "Noyek", have storage 900, he will be insert in the table and the new tables will be like this:
Lua:
local table = {1000, 900, 880, 720}
local pNames = {Smavel, Noyek, Quartz, Newbie

and the last player need to be removed (Newbie, 720), because table can contains only 3 players
Lua:
local table = {1000, 900, 880}
local pNames = {Smavel, Noyek, Quartz}
 
@roriscrave
First of all, how come you're using my second nickname as an example 🤔
Second of all, I would do it like that:
Code:
local howmanyplayersuwant = 3
local hmp = howmanyplayersuwant
local pNumbers = {1000, 880, 720}
local pNames = {Smavel, Quartz, Newbie}
    
x = getstorageoftheplayer
y = getnameoftheplayer
    
local check = hmp+1
    
function whatever(cid, whatever)
    for i = hmp, 1, -1 do --this checks what place in the table it should be
        if pNumbers[i] < x then
            check = check - 1
        end
    end
    
    for i = hmp, 1, -1 do --sorts everything
        local pNum
        local pName
        if check < i then
            pNumbers[i] = pNumbers[i-1]
            pNames[i] = pNames[i-1]
        elseif check == i then
            pNumbers[i] = x
            pNames[i] = y
            break
        end
    end
    
return true
end
 
Do you really need to have only 3 values per table and store the values on seperate tables? Because u could simply do this:
Lua:
t = {}
table.insert(t, {cid, storage})
table.sort(t, function(a, b) return a[2] > b[2] end)
for i = 1, math.min(#t, 3) do
    local data = {getPlayerName(t[i][1]), t[i][2]}
    print(data[1], data[2])
end
 
Last edited:
Do you really need to have only 3 values per table and store the values on seperate tables? Because u could simply do this:
Lua:
t = {}
table.insert(t, {cid, storage})
table.sort(t, function(a, b) return a[2] > b[2] end)
for i = 1, math.min(#t, 3) do
    local data = {getPlayerName(t[i][1]), t[i][2]}
    print(data[1], data[2])
end
wow, can u explain for min math.min? i never used this func
edit2: and is it possible to send a message to the player who left the table?
and also who entered, exemple:
in this case Smavel will leave and Flow will enter, is possible send a message for either:
Smavel: You leaved the table
Flow: You entered the table

Lua:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local t = {{"Smavel", 100}, {"Quartz", 200}, {"Newbie", 300}}
    table.insert(t, {"Flow", 250})
    table.sort(t, function(a, b) return a[2] > b[2] end)
    for i = 1, math.min(#t, 3) do
        local data = {(t[i][1]), t[i][2]}
        print(data[1], data[2])
    end
    return true
end
 
Last edited:
edit2: and is it possible to send a message to the player who left the table?
and also who entered, exemple:
in this case Smavel will leave and Flow will enter, is possible send a message for either:
Smavel: You leaved the table
Flow: You entered the table
What exactly you are attempting to? And its'a an action i suppose or globalevent?

wow, can u explain for min math.min? i never used this func

It takes the minimum value from 2 entries, in this case between #t (table count) and 3.
For example, if #t has a length of 2 it will take that 2 instead the 3, since 2 is the min value.

Same to the opposite, if #t has a length of 4, it will the take 3 instead the 4.
In short, the math.min will limit the 'for' only to 3 loops max.
 
What exactly you are attempting to? And its'a an action i suppose or globalevent?



It takes the minimum value from 2 entries, in this case between #t (table count) and 3.
For example, if #t has a length of 2 it will take that 2 instead the 3, since 2 is the min value.

Same to the opposite, if #t has a length of 4, it will the take 3 instead the 4.
In short, the math.min will limit the 'for' only to 3 loops max.
will be action, when u open a chest u gain certain storage (that`s in the table).
but i need if player enter in table he receive msg, and if player left, he receive too, exemple:

the table have this 3 player with highest storages:
Lua:
 local t = {{"Smavel", 100}, {"Quartz", 200}, {"Newbie", 300}}
if u have storage value 300, smavel will leave the table, so i need smavel receive a message ("You are not in table more, becase someone get more storage than you")
and quartz will pass to top 2 for top 3, he need receive message too ("You downgraded your position to 3")
 
Back
Top