• 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 Online command that shows the number of unique players.

Mr.Caffeine

Active Member
Joined
Nov 4, 2018
Messages
79
Reaction score
43
Hello. I would like an online command that shows the number of unique players not considering multi-clients characters.

Output:
There are currently xxx characters online.
There are currently xxx unique players online.

Does anyone have any idea how I can do this?

My !online script (tfs 0.4):
Lua:
local config = {
showGamemasters = getBooleanFromString(getConfigValue('displayGamemastersWithOnlineCommand'))
}
 
function onSay(cid, words, param, channel)
local players = getPlayersOnline()
local strings = {""}
 
local i, position = 1, 1
local added = false
for _, pid in ipairs(players) do
if(added) then
if(i > (position * 7)) then
strings[position] = strings[position] .. ","
position = position + 1
strings[position] = ""
else
strings[position] = i == 1 and "" or strings[position] .. ", "
end
end
 
if((config.showGamemasters or getPlayerCustomFlagValue(cid, PLAYERCUSTOMFLAG_GAMEMASTERPRIVILEGES) or not getPlayerCustomFlagValue(pid, PLAYERCUSTOMFLAG_GAMEMASTERPRIVILEGES)) and (not isPlayerGhost(pid) or getPlayerGhostAccess(cid) >= getPlayerGhostAccess(pid))) then
strings[position] = strings[position] .. getCreatureName(pid) .. ""
i = i + 1
added = true
else
added = false
end
end
 
doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, (i - 1) .. " players online.")

 
return true
end
 
Solution
Try this.

It will count each ip address as a unique player.

However, if people are mc'ing or 2 friends are using the same internet connection to play, they will only be counted as 1 unique player.

Lua:
local config = {
    showGamemasters = getBooleanFromString(getConfigValue('displayGamemastersWithOnlineCommand'))
}

function onSay(cid, words, param, channel)

    local unique_players_via_ip, character_count = {}, 0
  
    for _, pid in ipairs(getPlayersOnline()) do  
        if((config.showGamemasters or getPlayerCustomFlagValue(cid, PLAYERCUSTOMFLAG_GAMEMASTERPRIVILEGES) or not getPlayerCustomFlagValue(pid, PLAYERCUSTOMFLAG_GAMEMASTERPRIVILEGES)) and (not isPlayerGhost(pid) or getPlayerGhostAccess(cid) >= getPlayerGhostAccess(pid)))...
Try this.

It will count each ip address as a unique player.

However, if people are mc'ing or 2 friends are using the same internet connection to play, they will only be counted as 1 unique player.

Lua:
local config = {
    showGamemasters = getBooleanFromString(getConfigValue('displayGamemastersWithOnlineCommand'))
}

function onSay(cid, words, param, channel)

    local unique_players_via_ip, character_count = {}, 0
  
    for _, pid in ipairs(getPlayersOnline()) do  
        if((config.showGamemasters or getPlayerCustomFlagValue(cid, PLAYERCUSTOMFLAG_GAMEMASTERPRIVILEGES) or not getPlayerCustomFlagValue(pid, PLAYERCUSTOMFLAG_GAMEMASTERPRIVILEGES)) and (not isPlayerGhost(pid) or getPlayerGhostAccess(cid) >= getPlayerGhostAccess(pid))) then
            character_count = character_count + 1
            local ip, ip_found = getPlayerIp(pid), 0
            for i = 1, #unique_players_via_ip do
                if ip == unique_players_via_ip[i] then
                    ip_found = 1
                    break
                end
            end
            if ip_found == 0 then
                table.insert(unique_players_via_ip, ip)
            end
        end
    end
  
    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "There are currently " .. character_count .. " characters online.")
    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "There are currently " .. #unique_players_via_ip .. " unique players online.")
  
    return true
end
 
Solution
Try this.

It will count each ip address as a unique player.

However, if people are mc'ing or 2 friends are using the same internet connection to play, they will only be counted as 1 unique player.

Lua:
local config = {
    showGamemasters = getBooleanFromString(getConfigValue('displayGamemastersWithOnlineCommand'))
}

function onSay(cid, words, param, channel)

    local unique_players_via_ip, character_count = {}, 0
 
    for _, pid in ipairs(getPlayersOnline()) do 
        if((config.showGamemasters or getPlayerCustomFlagValue(cid, PLAYERCUSTOMFLAG_GAMEMASTERPRIVILEGES) or not getPlayerCustomFlagValue(pid, PLAYERCUSTOMFLAG_GAMEMASTERPRIVILEGES)) and (not isPlayerGhost(pid) or getPlayerGhostAccess(cid) >= getPlayerGhostAccess(pid))) then
            character_count = character_count + 1
            local ip, ip_found = getPlayerIp(pid), 0
            for i = 1, #unique_players_via_ip do
                if ip == unique_players_via_ip[i] then
                    ip_found = 1
                    break
                end
            end
            if ip_found == 0 then
                table.insert(unique_players_via_ip, ip)
            end
        end
    end
 
    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "There are currently " .. character_count .. " characters online.")
    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "There are currently " .. #unique_players_via_ip .. " unique players online.")
 
    return true
end
It works perfectly.
Thank you, Xikini. You help this community a lot.
 
Back
Top