• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

Lua When should I check storage using ipairs(getPlayersOnline())?

waqmaz

Member
Joined
Jun 17, 2015
Messages
203
Reaction score
11
Hello. I am confused a little.
When should I use
Code:
for _, cid in ipairs(getPlayersOnline())
?
I've asked, because this works the same:
Code:
            if(getPlayerStorageValue(cid, storage) == 1) then
                -- do something
            end
like this:
Code:
        for _, cid in ipairs(getPlayersOnline()) do
            if(getPlayerStorageValue(cid, storage) == 1) then
                -- do something
            end
        end
Should I use
for _, cid in ipairs(getPlayersOnline()) do
everytime I check storage? Does the server work faster with "pairs"?
I want to know that, because all my scripts do not use pair online players and yesterday I saw, someone used it.
Does it mean that if there is not used ipairs then script checks for all players in database and if there is ipairs then script checks only online players? If yes, then it is obvious that I should use ipairs almost always.
Thanks!
 
Well.. it depends where/what the script is being used for.
Code:
for _, cid in ipairs(getPlayersOnline()) do
This is going to loop through every single person online, and then do something.

So if it's only meant to be used for a single player, don't use that.
If your trying to check every person online for something, use that.
 
Example:
3 players have the same storage
In the example above, I do not use ipairs online, because those 3 players are treated as single players, yes?
I use ipairs only when I want to do something to every online players, yes?

/EDIT I think there should be used ipairs online, because they arent treated as single players, but many players.
 
You probably saw that in a global event. You use it when you have to, you should be able to tell if you need or not.

Just thinking: Do I have access to all the creatures(players in this case) that I want to use in my script? Where are they defined? How can I get them?...
 
Example:
3 players have the same storage
In the example above, I do not use ipairs online, because those 3 players are treated as single players, yes?
I use ipairs only when I want to do something to every online players, yes?

/EDIT I think there should be used ipairs online, because they arent treated as single players, but many players.
Yes and no.
Basically you only use it if your current script does not have all the information you need.

If we look at it a different way..
The function "getPlayerStorageValue(cid, storage)" can only look at 1 player at a time.

When you use "for _, cid in ipairs(getPlayersOnline()) do" you are effectively looping through each player individually, and each player runs through your script individually.

So when you use "for _, cid in ipairs(getPlayersOnline()) do" in conjunction with "getPlayerStorageValue(cid, storage) == 1", like in your script, your inside script '-- do something' is only going to effect the players online who have that storage value.

Hopefully this helps you understand.
 
What ^MatheusMkalo said.

If you wanna try to understand it on a deeper/less abstract level, I'll try to explain it in a way that makes it understandable if you have any basic lua knowledge:

There is something called 'cid', which can be thought of as Creature ID in this case. Every creature that exists in the game has their own 'cid' number, and they can be referred to, by using this number.
If you use getPlayerStorageValue(cid, key), it will return the value of the storage key OF this specific creature (the entered cid parameter).

So, how to use this?
Example: If a player has pulled lever A, upon pulling lever B, give him an item.

Doing something when a player uses something in the gameworld is achieved through action scripts. Each action script begins with an onUse function, which contains cid as a default parameter - meaning that, whenever a player uses something with an action script, we will automatically know their 'cid' and be able to use it in that script to refer to that player.

When pulling lever A, you would set the player's storage key 5555 to value '1'.
Code:
setPlayerStorageValue(cid, 5555, 1)

When pulling lever B, you would check if the player's storage key 5555 is equal to '1'. If it is, that means the player pulled lever A. Great, give him a reward.
Code:
if getPlayerStorageValue(cid, 5555) == 1 then
doPlayerAddItem(cid, 2160, 1)
end

So now, let's say, when a player uses lever A, every player that's online on the server will recieve an item. How do you do that, if you only know the 'cid' of the player that used the lever, but not the 'cids' of all other players that are online?
Here you use getPlayersOnline(). This function returns a table with the cids of every person that's online which looks like this basically: {cid1, cid2, cid3, cid4, ...}

Let's store this entire array into a variable called allPlayers.
Code:
local allPlayers = getPlayersOnline()

Now we loop through this entire array, and we do something for every cid found in this array.

Code:
for _, cid in ipairs(allPlayers) do
if(getPlayerStorageValue(cid, storage) == 1) then
doPlayerAddItem(cid, 2160, 1)
end
end

Here's some more info on the FOR loop itself, if you or anyone else is interested:

If we break down the first line of the loop, we will notice 3 parameters:

for key, value in ipairs(table) do

Key, value and table are things that declare what this loop will be looping through.
Key is a 'name' by which we will refer to the current key in the table that's being checked.
Value is a 'name' by which we will refer to the value that's being checked.

Two common ways of structuring tables are:
1) table = { [key] = value, [key] = value, [key] = value }
2) table = { value, value, value, value }

In our case, the getPlayersOnline() function gives us a table:
Code:
getPlayersOnline() = {cid1, cid2, cid3, cid4, ...}  --- This is a table with only values, no keys. (structure #2)

Commonly, using underscore (the _ sign) for a name of Key or Value means that we won't be having any use of that parameter during this loop, so we're kinda nixing it.
Since we're only interested in the values of this table, not keys, we will give these values a name.
Since all of these values are someone's 'cids', we give them the name 'cid'. It doesn't have to be cid, it could've been anything we choose, but we chose 'cid' for convenience.

And finally, in the brackets next to ipairs, you put the table which you want to loop through. Hence, our loop looks like this:

Code:
for _, cid in ipairs(allPlayers) do
-- something
end

So now, this loop is basically starting at the beginning of the allPlayers table, saying okay, I found 'cid1' at the first place of the table, I will give this 'cid1' the variable name you wanted me to give it (which is 'cid', remember). Now cid is equal to cid1 , do you wanna do something with it?

When it reaches the 'end'. It ditches cid1, goes to the next value which is cid2, makes cid = cid2, and again - do you wanna do something with this cid?
When it reaches the 'end', it moves from cid2 to cid3, etc. Until it reaches the end of the entire table.

So if you want to do something to every player that's online right now, inside of this loop is where you'd do it.

Code:
-- Giving every players that's online an item, if their storage key 5555 is on value 1
for _, cid in ipairs(allPlayers) do
if(getPlayerStorageValue(cid, 5555) == 1) then
doPlayerAddItem(cid, 2160, 1)
end
end
 
Last edited:
Back
Top