In response to:
I have decided to make this short tutorial (from other linked websites and one in here for os.time()) because many people has asked for it.
addEvent "tutorial": How to use addEvent()?
Storage values: Storage Values - OpenTibia Fans (note that the example was for XML servers, now it's saved in database)
How to know e.g. if an hour has passed since a certain action:
os.time() - this function will return a time that increase each second (it's seconds since 1970, but that's irrelevant). By knowing it increases each second, it also means that it has increased by 60 in a minute...
That way, you can put the os.time() in a storage value, and next time he use the item/talkaction, it will compare the current os.time() with the stored os.time().
Example:
Notepad++ is a must-have: .:: NOTEPAD++ ::.
Regards,
Colandus
I learned how to script now, much thansk to you guys, just need a tutorial how to add time etc and a bit more things...
I have decided to make this short tutorial (from other linked websites and one in here for os.time()) because many people has asked for it.
addEvent "tutorial": How to use addEvent()?
Storage values: Storage Values - OpenTibia Fans (note that the example was for XML servers, now it's saved in database)
How to know e.g. if an hour has passed since a certain action:
os.time() - this function will return a time that increase each second (it's seconds since 1970, but that's irrelevant). By knowing it increases each second, it also means that it has increased by 60 in a minute...
That way, you can put the os.time() in a storage value, and next time he use the item/talkaction, it will compare the current os.time() with the stored os.time().
Example:
Code:
local compareTime = 5 * 24 * 60 * 60 -- This is 5 days, in seconds. I will compare the differences of the stored os.time() with the current os.time() to see how long time ago it was stored.
local currentValue = getPlayerStorageValue(cid, 23232)
if currentValue == -1 then -- This means that it has not been set before. If I'd compare -1 with os.time it'd give me the seconds of 39 years (2009 - 1070 = 39) and it would cause my statements to fail. So what I need to do is to declare it as os.time(). This will only happen the first time.
currentValue = os.time()
end
if (os.time() - currentValue) >= compareTime then -- This will check if the last time you did this action (e.g used item or executed talkaction) was more than 5 days ago (compareTime) by subtracting the current time with the old time.
-- Now, we do something that should only be able to be done each 5 days, e.g. give an item.
doPlayerAddItem(cid, itemid, count)
-- Now, we need to update the stored time, so next time he use it, it should count that he has done this quest right now.
setPlayerStorageValue(cid, 23232, os.time()) -- Storage value must obviously be the same as the one we check. We set it to os.time() to store the current time.
else
-- The statement failed, which means that he done the quest within 5 days, he still need to wait.
doPlayerSendCancel(cid, "You are not ready for this yet.")
end
Notepad++ is a must-have: .:: NOTEPAD++ ::.
Regards,
Colandus