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