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

function getCurrenPlayers + Example

Shawak

Intermediate OT User
Joined
Sep 11, 2008
Messages
1,984
Solutions
2
Reaction score
119
Location
Germany
GitHub
Shawak
Hello all!

Here a usefull function: getCurrentPlayers().

To add it go to your data/global.lua or your data/lib/function.lua and add:
Lua:
function getCurrentPlayers() -- by Shawak + Colandus
        local db_player = db.getResult("SELECT `name` FROM `players` ORDER BY `name` ASC;")
        local players = {}

        if (db_player:getID() ~= -1) then
		repeat
			table.insert(players, db_player:getDataString("name"))
		until not db_player:next() 
        end
        db_player:free()
        return players
end

Example:
Lua:
	local players = getCurrentPlayers()

	print(">> The server have "..#players.." players.")
	
	for i = 1, #players do
		print("> "..players[i])
	end

I hope you like it! ^_^

Regards,
Shawak
 
Last edited:
Resource thief !!

First of all, nobody will ever want to display all players ! But if they will use this function to count players it will take much resources, so it's waay better to simply use COUNT...

Also I've been trying to teach everyone to stop using while-loops for looping database content ! Use instead:
Lua:
repeat
        table.insert(players, db_player:getDataString("name"))
until not db_player:next()

It is equivalent to C++ "do {block} while(statement);"
 
Resource thief !!

First of all, nobody will ever want to display all players ! But if they will use this function to count players it will take much resources, so it's waay better to simply use COUNT...

Also I've been trying to teach everyone to stop using while-loops for looping database content ! Use instead:
Lua:
repeat
        table.insert(players, db_player:getDataString("name"))
until not db_player:next()

It is equivalent to C++ "do {block} while(statement);"

The players displaying was just an example.

@Script: Changed the loop to the repeat function :p.
 
What I meant was that it's totally pointless doing it this way. It take a lots of resources and won't even be used. If you instead make the query only show the amount of players it's better, because that's the only way you will need it.
 
What I meant was that it's totally pointless doing it this way. It take a lots of resources and won't even be used. If you instead make the query only show the amount of players it's better, because that's the only way you will need it.

As I said, this was one example, you can easy use the function to make other things.
 
@Shawak:
Show me this other thing :p
 
Last edited:
What I meant was that it's totally pointless doing it this way. It take a lots of resources and won't even be used. If you instead make the query only show the amount of players it's better, because that's the only way you will need it.

Other way to use:

Lua:
	local players = getCurrentPlayers()
	local text = "Current registred players:"

        for i = 1, #players do
                text = text.."\n"..i..". "..players[i] 
        end

	doPlayerPopupFYI(cid, text)
 
It's still the same. You don't get what I mean? Supposed there's 10000 players registered on the server. Now that function will load 10000 players each time and also put them in a table and then you will loop that table.

I meant the whole function is a waste of resources no matter usage.
 
It's still the same. You don't get what I mean? Supposed there's 10000 players registered on the server. Now that function will load 10000 players each time and also put them in a table and then you will loop that table.

I meant the whole function is a waste of resources no matter usage.

Then don't use it if you don't like it. :)
And if you think you can do it better, do it.
 
I was expecting that answer ;) You're the guy who doesn't want to understand shit... I told you ITS WASTE OF RESOURCES... Do you understand what that means ? It means your server will be veeery slow using that code.

There will be no time you will ever need a list of all players, but to count them you could simply use:
Lua:
local db_player = db.getResult("SELECT COUNT(`id`) FROM `players`)

It's also not the best solution, however, a better one would be to save the amount of players when they are created.
 
I was expecting that answer ;) You're the guy who doesn't want to understand shit... I told you ITS WASTE OF RESOURCES... Do you understand what that means ? It means your server will be veeery slow using that code.

There will be no time you will ever need a list of all players, but to count them you could simply use:
Lua:
local db_player = db.getResult("SELECT COUNT(`id`) FROM `players`)

It's also not the best solution, however, a better one would be to save the amount of players when they are created.

You just flaming about my example, easy ceate a new thread and name it "function getCurrentPlayerCount()".
 
oooooooooooooooooooooooooooooooommmmmmmmmmmmmmmmmmmgggggggggggggggggggggggggggggggg

flaming ??? im fucking wasting my time trying to teach you something and u come up with that bullshit ?? well then dont expect any further help or comments from me !
 
oooooooooooooooooooooooooooooooommmmmmmmmmmmmmmmmmmgggggggggggggggggggggggggggggggg

flaming ??? im fucking wasting my time trying to teach you something and u come up with that bullshit ?? well then dont expect any further help or comments from me !

Then show me a better way to get a list with all players.., not player count.
 
welllllll....... you shouldn't get the list at all as you wont need it....

you don't need to see everything as a threat against yourself, that way you wont go far....
 
welllllll....... you shouldn't get the list at all as you wont need it....

you don't need to see everything as a threat against yourself, that way you wont go far....

But if you need it you have a function, :).
 
suuuuure i wont comment no moooooore

i was just tryinnnnng to teeeeeach youuuuuuuuuu sometttttttthinnnnnnnnggggggggggggggg
 
You trying to teach me how to count the players, but my function should show the player names :thumbup:.

@Thread: Pls more Comments!?
 
hm never seen before two advanced scripters fighting :D go colan!

@shawak, getWorldCreatures(type)? well maybe not names dunno
 
Back
Top