• 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!
  • New resources must be posted under Resources tab. A discussion thread will be created automatically, you can't open threads manually anymore.

TalkAction !online viewed as a book, TFS 1.1

Marcus

User.postCount++;
Joined
Nov 14, 2015
Messages
1,074
Solutions
10
Reaction score
392
Location
Sweden
For those who rather have their online list in a book rather than a spamming message over the console, you can simply use this code I made.

Enjoy it!

talkactions.xml
PHP:
<talkaction words="!online" script="online.lua" />

online.lua
PHP:
function onSay(player, words, param)
   local players = Game.getPlayers()
   local playerCount = Game.getPlayerCount()

   player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, playerCount .. " players online.")

   local i = 1
   local msg = ""
   for k, targetPlayer in ipairs(players) do
       msg = msg .."[".. i .."] ".. targetPlayer:getName() .. " [" .. targetPlayer:getLevel() .. "]\n"
       i = i + 1
   end

   player:showTextDialog(1978, msg)
   return true
end
 
Code:
function onSay(player, words, param)
    local playersOnline = Game.getPlayers()
  
    player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, ( #playersOnline > 1 and "There are " .. #playersOnline .. " players online." or "You are the only player online."))

    if #playersOnline > 1 then
        local msg
        for i, p in ipairs(playersOnline) do
            msg = msg .."[".. i .."] ".. p:getName() .. " [" .. p:getLevel() .. "]\n"
        end
        player:showTextDialog(1978, msg)
    end
    return true
end
 
Last edited:
player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, ( #playersOnline > 1 and "There are " .. #playersOnline .. " players online." or "There are no players online."))
If there would be 1 player online, it would say "There are no players online", I am wrong?
Isn't there a = missing?
 
Code:
function onSay(player, words, param)
    local playersOnline = Game.getPlayers()
  
    player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, ( #playersOnline > 1 and "There are " .. #playersOnline .. " players online." or "There are no players online."))

    if #playersOnline > 1 then
        local msg
        for i, p in ipairs(playersOnline) do
            msg = msg .."[".. i .."] ".. p:getName() .. " [" .. p:getLevel() .. "]\n"
        end
        player:showTextDialog(1978, msg)
    end
    return true
end
Kinda missing the point of your comment. You're just copying the majority of his code and posting it back to him on his own release. Plus @Danger II is correct, if there's 1 player online, it won't show any players in the list and it will say there are no players online. Sure, if there's only one person, he doesn't really need to see himself in the list, but it still should be there. After all, there IS a player online.
 
bad to concat to string each loop, better to make an empty table and insert each string to the table and concat at the end
 
bad to concat to string each loop, better to make an empty table and insert each string to the table and concat at the end
I don't see how you can avoid concatenating within a loop by doing this, you still have to run a loop to put the strings into the table and if you want the strings to be accurate, you're gonna have to concatenate them... Explain yourself
 
Kinda missing the point of your comment. You're just copying the majority of his code and posting it back to him on his own release. Plus @Danger II is correct, if there's 1 player online, it won't show any players in the list and it will say there are no players online. Sure, if there's only one person, he doesn't really need to see himself in the list, but it still should be there. After all, there IS a player online.
Thanks, yes I copied the majority of his which was the point I just made it simpler and as Danger pointed out yes there is a player online how else will this talkaction be executed if there wasn't.

Another solution would be to change the text, "There are no players online" to "You are the only player online".

I made the change just for you two <3

bad to concat to string each loop, better to make an empty table and insert each string to the table and concat at the end
Actually you are incorrect in your assumption in this case.
 
I don't see how you can avoid concatenating within a loop by doing this, you still have to run a loop to put the strings into the table and if you want the strings to be accurate, you're gonna have to concatenate them... Explain yourself
Code:
for i=1,100 do
str = str.."concat"
end
is slower than
Code:
local t = {}
for i=1,100 do
t[i] = "concat"
end
return table.concat(t, "")

@Tobas
Code:
local t = {}
local str = ""
local time = os.clock()
for i=1,50000 do
str = str..i
end
print(os.clock()-time)
time = os.clock()
for i=1,50000 do
t[i] = i
end
local conc = table.concat(t, "")
print(os.clock()-time)
good example that showcases this, first case takes 0.58s, second 0.01s
 
Code:
for i=1,100 do
str = str.."concat"
end
is slower than
Code:
local t = {}
for i=1,100 do
t[i] = "concat"
end
return table.concat(t, "")

@Tobas
Code:
local t = {}
local str = ""
local time = os.clock()
for i=1,50000 do
str = str..i
end
print(os.clock()-time)
time = os.clock()
for i=1,50000 do
t[i] = i
end
local conc = table.concat(t, "")
print(os.clock()-time)
good example that showcases this, first case takes 0.58s, second 0.01s
Sigh..
 
you are not going to have 50000 payers online in the same server.
If I have to choose between 0.01 seconds or less code lines. I choose less code lines.

because it takes way more time for human to read and understand 1 line of code, even if it is just setting variable.
 
I just wanted to add one final thing, The for loop and text box do not execute unless there are more than 1 players on, yet everyone wants to focus on the fraction of a second saved between using table.concat over concatenating a string, you people really need to get a life, if only you used that wasted energy on an actual skill-set instead of copy and paste or monkey see monkey do activities, sorry but trolling every thread you disagree with is not a skill-set.
 
I just wanted to add one final thing, The for loop and text box do not execute unless there are more than 1 players on, yet everyone wants to focus on the fraction of a second saved between using table.concat over concatenating a string, you people really need to get a life, if only you used that wasted energy on an actual skill-set instead of copy and paste or monkey see monkey do activities, sorry but trolling every thread you disagree with is not a skill-set.
code wont even work anyways
didnt make a base string to concat with
me troll me right
 
you are not going to have 50000 payers online in the same server.
If I have to choose between 0.01 seconds or less code lines. I choose less code lines.

because it takes way more time for human to read and understand 1 line of code, even if it is just setting variable.
Code:
 function onSay(player, words, param)
    local playersOnline = Game.getPlayers()
  
    player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, ( #playersOnline > 1 and "There are " .. #playersOnline .. " players online." or "You are the only player online."))

local tmp = {}
    if #playersOnline > 1 then
        local msg
        for i, p in ipairs(playersOnline) do
            tmp[i] = "[".. i .."] ".. p:getName() .. " [" .. p:getLevel() .. "]\n"
        end
        player:showTextDialog(1978, table.concat(tmp))
    end
    return true
end
it is literally a matter of adding a temporary table to use, are you really saying you want to have way worse performance on a script because you dont want to have a temporary variable to read?
sure, you might not have enough players for it to matter, but what about the case if someone tries to attack your server? many servers have REALLY bad !online scripts, so spamming that on multiple characters might make it actually noticeable

now if you REALLY cared about performance, you would use some cache-based !online system instead of generating it every time the action is called
 
Back
Top