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

GlobalEvent Send text and effect to the Top Level ~ TFS 1.3 Revscripts

Sarah Wesker

ƐƖєgαηт Sуηтαx ❤
Staff member
TFS Developer
Support Team
Joined
Mar 16, 2017
Messages
1,418
Solutions
155
Reaction score
1,975
Location
London
GitHub
MillhioreBT
Twitch
millhiorebt
data/scripts/showtoplevel.lua
Lua:
local config = {
    interval = 10 * 1000, -- 10 seconds
    text = "[Top Level]",
    effect = CONST_ME_HEARTS
}

local event = GlobalEvent("TopLevelEffect")

function event.onThink(interval)
    local resultId = db.storeQuery("SELECT name FROM players WHERE group_id < 2 ORDER BY level DESC LIMIT 1;")
    if not resultId then
        return true
    end

    local player = Player(result.getString(resultId, "name"))
    if player then
        local position = player:getPosition()
        position:sendMagicEffect(config.effect)
        player:say(config.text, TALKTYPE_MONSTER_SAY, false, nil, position)
    end

    result.free(resultId)
    return true
end

event:interval(config.interval)
event:register()
 
Not bad at all! Is it possible to make it change without the player having to relog for another player to take the "title"?
 
I wouldn't consider this proper implementation, you would be better off with iterating all players in the game via getPlayersOnline. It should be way faster, cause it access memory and not disc space, but I see it's done every 10 seconds (lowering the interval could hurt though), so I guess it doesn't matter? 🤔
 
What i would done, which may occur as a overkill of Micro optimizations. I would on game startUp fetch top level from the database. Store it , compare and watch over as Nekiro metion those who are online

Update
Even better trying to make it work with onAdvance
 
What i would done, which may occur as a overkill of Micro optimizations. I would on game startUp fetch top level from the database. Store it , compare and watch over as Nekiro metion those who are online

Update
Even better trying to make it work with onAdvance
Yup, kinda overkill, but sounds like a pretty good caching system.
 
Hello everyone, thanks for commenting on this thread I did it this way for the interval of 10 seconds it seemed to me that there is enough time so that it does not cause any negative effect on the server

I will try to improve it ;)
 
Yup, kinda overkill, but sounds like a pretty good caching system.
Haha yep 😅
Hello everyone, thanks for commenting on this thread I did it this way for the interval of 10 seconds it seemed to me that there is enough time so that it does not cause any negative effect on the server

I will try to improve it ;)
Love the attitude, you go 🙌🧠
 
That query is completely unnecessary, just use Game.getPlayers.

Lua:
local bestPlayer = {id = 0, level = 0}

local function checkBestPlayer()
    local bestLevel = 0
    local bestId = nil
  
    local players = Game.getPlayers()
    for _, player in ipairs(players) do
        if player:getGroup():getId() < 2 then
            local level = player:getLevel()
            if level > bestLevel then
                bestLevel = level
                bestId = player:getId()
            end
        end
    end
  
    bestPlayer = {
        level = bestLevel,
        id = bestId
    }
end

function onThink()
    local player = Player(bestPlayer.id)
    if player then
        -- send effect, say etc.
    else
        checkBestPlayer()
    end
end
 
@oen432 actually, if you do this way, it will work for current top level online only. If top level is offline, another player will have this effect.
Oh, that's what this is supposed to be, best player on the server, not best player that is online.
Lua:
local bestPlayer = nil

-- get best player by level from database and cache it
local function checkBestPlayer()
    local resultId = db.storeQuery("SELECT id, level FROM players WHERE group_id < 2 ORDER BY level DESC LIMIT 1")
    if not resultId then
        return
    end
  
    bestPlayer = {
        id = result.getNumber(resultId, "id"),
        level = result.getNumber(resultId, "level")
    }
  
    result.free(resultId)
end

function onLogin()
    if not bestPlayer then
        checkBestPlayer() -- when first player logins, get best player from database and cache it
    end
end

function onAdvance(player, skill, oldLevel, newLevel)
    if skill == SKILL_LEVEL then
        if bestPlayer and newLevel > bestPlayer.level then -- if someone advances in level and is now higher level than bestPlayer, make him new bestPlayer
            bestPlayer.level = newLevel
            bestPlayer.id = player:getId()
        end
    end
end

function onThink()
    local player = Player(bestPlayer.id)
    if player then
        -- send effect, say etc.
    end
end
 
Reading this post, I already had something prepared I wanted to say about the topic of using the onThink being a bad or "incorrect" implementation.


top level is offline, another player will have this effect.
That is what I was gonna say :D


@Sarah Wesker , brilliant anyway it goes! Simple, and effective!
 
Back
Top