• 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 Involving time in scripts

Colandus

Advanced OT User
Senator
Joined
Jun 6, 2007
Messages
2,434
Solutions
19
Reaction score
219
Location
Sweden
In response to:
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
 
I can use os.time even for values which more then 24 hours? I thouth after 24h it's will be same cycle, like if I will jail someone for 25 hours, at 14:15, it will store 15:15, and jailing will gone in 1h instead of 25 hours....

or it's when os.date("%Y-%m-%d %H:%M:%S") I can set values even for years?xD
 
Last edited:
os.time() is since 1970, so I bet 24 hours is no problem ;)

Actually os.time() stores the seconds since 1970, so after 1 year it will hold (60 * 60 * 24 * AMOUNT_OF_DAYS_PER_YEAR) and all you do is to compare the current os.time() with the stored one :)
 
Last edited:
aff i forgot that shit :( i cant repost i dont have saved and i dont feel like rewriting :p
 
Back
Top