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

TFS 1.0 - how check only active players?

giddy92

New Member
Joined
Aug 8, 2014
Messages
69
Reaction score
0
I need command like:
!online
for checking only players whos no afk.
It is possible?
 
Solution
Not sure how you want to detect afk players, but you could do something like this:

It checks the position of all players, and saves a timestamp storage and their current position everytime they are detected as active.

An inactive player is recognized by having the same current position as last time he was active.
An active player is recognized by having different current position than last time he was active.

Make sure the 4 storages (timestampStorage, posXstorage, posYstorage and posZstorage) are using a unique id not used anywhere else.

Globalevent:
Lua:
-- Globalevent script, run it etc every 1 or 5 min.
function onThink(interval)
    -- timestamp = timestamp on when player was last active.
    local timestampStorage = 31515
    --...
Not sure how you want to detect afk players, but you could do something like this:

It checks the position of all players, and saves a timestamp storage and their current position everytime they are detected as active.

An inactive player is recognized by having the same current position as last time he was active.
An active player is recognized by having different current position than last time he was active.

Make sure the 4 storages (timestampStorage, posXstorage, posYstorage and posZstorage) are using a unique id not used anywhere else.

Globalevent:
Lua:
-- Globalevent script, run it etc every 1 or 5 min.
function onThink(interval)
    -- timestamp = timestamp on when player was last active.
    local timestampStorage = 31515
    -- pos storages = which position the player was in last time he was active.
    local posXstorage = 31516
    local posYstorage = 31517
    local posZstorage = 31518
    local players = Game.getPlayers() -- All online players

    -- Loop through all online players
    for k, tmpPlayer in ipairs(players) do
        -- Get player current position
        local playerPosition = tmpPlayer:getPosition()

        -- Check if current position == old position
        if playerPosition.x == tmpPlayer.getStorageValue(posXstorage) and playerPosition.y = tmpPlayer.getStorageValue(posYstorage) and playerPosition.z = tmpPlayer.getStorageValue(posZstorage) then
            -- Same position, considered afk. Do nothing.
        else
            -- User is active, save timestamp and new position
            -- Set timestamp
            tmpPlayer.setStorageValue(timestampStorage, os.time())
            -- Set new positions
            tmpPlayer.setStorageValue(posXstorage, playerPosition.x)
            tmpPlayer.setStorageValue(posYstorage, playerPosition.y)
            tmpPlayer.setStorageValue(posZstorage, playerPosition.z)
        
        end
    end
    return true
end

Talkaction:
Lua:
function onSay(cid, words, param)
    local player = Player(cid) -- The player that triggered this script
    local players = Game.getPlayers() -- All online players
    local playerCount = Game.getPlayerCount() -- Count of all players
    local activeplayers = 0; -- Active players
    local consideredafk = (8 * 60) -- You are considered afk/inactive after 8 minutes.
    local msg = "" -- Build up the response text
    -- timestamp = timestamp on when player was last active.
    local timestampStorage = 31515

    -- Loop through all online players
    for k, tmpPlayer in ipairs(players) do
        -- Fetch the storage value they have saved timestamp in
        local playerTimestamp = tmpPlayer.getStorageValue(timestampStorage)

        -- If timestamp on the player is not older than configured afk timer
        if playerTimestamp > os.time() - consideredafk then
            -- Sum the active player
            activeplayers = activeplayers + 1
            -- Append his name to the response text
            msg = msg .. tmpPlayer:getName() .. ", "
        end
    end
    -- Prepend how many players are active before listing the names
    msg = "Active players: " .. activeplayers .. ". Names: " .. msg

    -- Send the response text
    player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, msg)

    return false
end
 
Solution
Back
Top