• 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 help in function

gabriel28

Member
Joined
Mar 16, 2012
Messages
199
Solutions
6
Reaction score
24
I tried to make a new function called 'setPartyStorage' that give a storage value to all party members (include the leader, ofc), but I don't have to much knowledge, so, I need you help to make it work.
The problem is that I try to get all members, but I'm unsuccessful.
Here are my try:
Lua:
function setPartyStorage(pt, key, value)
    local pt = getPlayerParty(cid)
    local memb = getPartyMembers(pt)
    local pid = getPlayerGUID(cid)
    for _, pid in pairs(memb) do
        local getvalue = db.getResult("SELECT `value` FROM `player_storage` WHERE `player_id` = " .. pid .. " and `key` = " .. key .. " LIMIT 1;")
        if(getvalue:getID() ~= -1) then
            db.executeQuery("UPDATE `player_storage` SET `value` = " .. pid .. " WHERE `key`=" .. key .. " LIMIT 1');")
            getvalue:free()
            return
        else
            db.executeQuery("INSERT INTO `player_storage` (`player_id`, `key`, `value`) VALUES (" .. pid .. ", " .. key .. ", '"..value.."');")
            return
        end
    end  
end

Thank you in advance.
 
Solution
Any changes to players storage via database will be discarded once the player relog
This one should work
Lua:
function setPartyStorage(cid, key, value)
local party = getPlayerParty(cid) or 0
if party ~= 0 then
    for _, pid in pairs(getPartyMembers(party)) do
        doCreatureSetStorage(pid, key, value)
    end
end
Any changes to players storage via database will be discarded once the player relog
This one should work
Lua:
function setPartyStorage(cid, key, value)
local party = getPlayerParty(cid) or 0
if party ~= 0 then
    for _, pid in pairs(getPartyMembers(party)) do
        doCreatureSetStorage(pid, key, value)
    end
end
 
Solution
@SpiderOT
I thought of doing it that way (without accessing the database), but I thought to myself, "I guess that way it will not work the way I want it to."
So I set out for the more "complex" form. hahahaha
I have to start thinking that not every complex script will be good for what I want.
Thank you very much, I'll take this thought forward when I go build my next scripts.
 
@SpiderOT
I'm sorry if I'm bothering, but I need to remove this doubt, since I've seen that in the system I'm doing, just setting up a storage for all members of the party will not be enough, since the player, even with the storage, can enter another party and do the event again. So I need to get storage value of the party members.

So I did two functions:
first (not get the value)
Lua:
function getPartyMemberStorageValue(cid, key)
local party = getPlayerParty(cid)
    if party then
    for _, pid in pairs(getPartyMembers(party)) do
        return getPlayerStorageValue(pid, key)
    end     
    end
end

second (this one work, I think, not sure)
Lua:
function getPartyMemberStorageValue(cid, key)
local valor = db.getResult("SELECT `value` FROM `player_storage` WHERE `player_id` = " .. getPlayerGUID(cid) .. " and `key` = " .. key)
local party = getPlayerParty(cid)
    if valor:getID() == -1 then
        return
    end
    local sto_value = valor:getDataInt('value')
    if party then
            getPlayerStorageValue(party, key)
    valor:free()
    return sto_value
    end
end

So, I try to get the name of the player in the party who have the storage value higher than 0 with:
Lua:
if getPartyMemberStorageValue(cid, storage) > value then
    selfSay("The player ".. getCreatureName(cid, (getPartyMemberStorageValue(cid, storage) > value)) .." have did it today. Remove him from the party to continue.", cid)
    npcHandler:releaseFocus(cid)
    return true
end
But without success. Can you give me a tip?
 
Last edited:
@gabriel28 Feel free to post any doubts
Since we are dealing with an online player getting his info via database would be the last thing to think about(at least for me)
Assuming you want the players to make this event several times with a delay of xHours then you should use exhaust
something like this

Lua:
function setPartyStorage(cid, key, value, exhaust, delay)--delay in hours
local party = getPlayerParty(cid) or 0
if party ~= 0 then
    for _, pid in pairs(getPartyMembers(party)) do
        if exhaustion.check(pid, exhaust) == true then
            doCreatureSetStorage(pid, key, -1)
        else
            doCreatureSetStorage(pid, key, value)
            exhaustion.set(pid, exhaust, delay*60*60)
        end
    end
end
end

function getPartyMemberStorageValue(cid, key, exhaust)
local playersTbl, playersStr = {}, ""
local party = getPlayerParty(cid) or 0
if party ~= 0 then
    for _, pid in pairs(getPartyMembers(party)) do
        if exhaustion.check(pid, exhaust) == true then
            table.insert(playersTbl, pid)
            playersStr = ""..playersStr..""..getCreatureName(pid)..","
        end
    end
end
    return playersTbl, playersStr
end

local badplayersTable,badplayersString = getPartyMemberStorageValue(cid, key, exhaust)    --return table of the players and string of their names

if badplayersString ~= "" then
    selfSay("The following players have done this event today. Remove them from the party to continue ".. badplayersString.."", cid)
    npcHandler:releaseFocus(cid)
    return true
end

since the setPartyStorage function now reset the player storage to -1 you can actually ignore the getPartyMemberStorageValue part and add this part to your npc
Lua:
local exhaust_storage = 17000
local party = getPlayerParty(cid) or 0
if party ~= 0 then
    for _, pid in pairs(getPartyMembers(party)) do
        if exhaustion.check(pid, exhaust_storage) ~= true and getPlayerStorageValue(pid, key) == value then
            doTeleportThing(pid, pos)--or whatever your code is
        end
    end
end
 
Last edited:
@SpiderOT Thank you so much for teaching me.
I keep insisting on getting the information in the database even if you've touched on it before. hahaha (It's because after I did some functions that get information about the health, mana, players' skills straight from the database (after I saw that using functions like 'getCreatureMaxHealth', etc, end up bugging if the player uses any item that changes that value ) always comes in mind fetching information from there, eventually becoming an addiction xD).

But, anyway, I have tested here and it is working smoothly ^^. But I need to ask some questions because I have some doubts:

First:
This part:
local exhaust_storage = 17000 local party = getPlayerParty(cid) or 0 if party ~= 0 then for _, pid in pairs(getPartyMembers(party)) do if exhaustion.check(pid, exhaust_storage) ~= true and getPlayerStorageValue(pid, key) == value then doTeleportThing(pid, pos)--or whatever your code is end end end
Will don't return the table with the players names that are in exhaust time, because, as I saw, the table is generated with the getPartyMemberStorageValue function. So, if I need to make an alert to the party leader about the players that can't go with them, I will need to use the getPartyMemberStorageValue function, right?

Second:
That way it's done, I can't put in a lib to I don't have to keep repeating all these lines in each new script, since that way they are setting up a specific storage (Correct me if I wrong). With the 'setPartyStorage' function, I have idea how to use:
Lua:
local config = {
sto = XXX,
exhaust = YYY
}
CODE...
setPlayerStorageValue (cid, config.sto, config.exhaust)
CODE..
But with the 'getPartyMemberStorageValue' function, I have no ideia how to do it. Because, from what I understand, the way it's written, can't put it in a lib and make it check different storages, right?

Well, again, thank you so much. I will sleep now (is 4 a.m here in Brazil, and I spent the day trying to make it xD)
When I wake up, I will read your answer. (Sorry for my Google Translated english :D)
 
As for the first part, yes you will need it
and for the second part, I've updated the above post to pass exhaust storage to functions too
 
@SpiderOT
Thank you.
Can you clarify (I'm out of my home, so I can't test it now to clarify by myself) the variables exhaust and delay of function setPartyStorage(cid, key, value, exhaust, delay) for me?
The 'delay' I have idea, will be the number of hours of exhaust to can do something again, right? But the ehxaust itself?

To set will be: setPartyStorage(cid, 1111[the storage], 1[to 'active' the storage], exhaust [?], 24[to be 24 hours])
And getPartyMemberStorageValue will be:
getPartyMemberStorageValue(cid, 1111[the storage], exhaust[the same as in setPartyStorage])
Right?
 
@SpiderOT
Oh, I see. So, will be one to other uses, if I want, and one to storage the exhaustion time. And with it, I can use getPartyMemberStorageValue to check both storages, if I want to make a quest that is done once or a daily quest (or whatever).

Because of it, I be able to make another function that get players party level.

Code:
function getPartyPlayerLevel(cid, lvl)
local party = getPlayerParty(cid) or 0
if party ~= 0 then
    for _, pid in pairs(getPartyMembers(party)) do
        if getPlayerLevel(pid) < lvl then
            return true
        end   
    end
end
end

So, if I want to block players with level lower than 100, I use function getPartyPlayerLevel(cid, 100)
Thank you again.
 
Last edited:
Back
Top