• 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 os time () storage set for a time

ellvo

Advanced OT User
Joined
Sep 23, 2016
Messages
1,189
Reaction score
169
As guys under corrected me,reposting

I would need some help with os time() how it is working like or exactly,I need help with something like I need to set storage 9999,1 for 1 minute how to do that.

if player:getStorageValue(56148) < 0 then
player:setStorageValue(cid, 56148, os.time()+ 1 * 60) -- (it is default -1 need to make it 1 idk how)
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'You may enter this tp again in 1 minute.')
end

any ideas ?

if player:getStorageValue(56148) == 1 then
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'It is less then one minute you have entered the tp.')
end
 
Last edited:
Solution
Code:
local lastTime = player:getStorageValue(56148)

local now = os.time()

-- If lastTime is -1 or 0 or any other low number, timePassed will be a long number rather than the exact time passed
-- This means we can safely check if timePassed > x even if lastTime was 0 or -1
local timePassed = now - lastTime

if timePassed >= 60 then
        player:setStorageValue(56148, now)
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'You may enter this tp again in 1 minute.')
else
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'It is less then one minute you have entered the tp.')
end


Or we could do it your way and add time directly to storage (no big difference, but I prefer above way):
Code:
local lastTime =...
don't just change thread title and edit your post, read the rules
if you find a solution to your problem, let the thread be and post the solution incase someone else needs it.

+ if you want a thread deleted just report your own post and put delete in the reason
 
don't just change thread title and edit your post, read the rules
if you find a solution to your problem, let the thread be and post the solution incase someone else needs it.

+ if you want a thread deleted just report your own post and put delete in the reason
+1 for this

Any mistake you can make, someone else can reproduce. You'll find I have two or three threads where I answer my own question.
 
os.time() returns amount of seconds what have passed since 1970-01-01 00:00, becouse of big numbers here - 32bit systems will be not able to handle date after 2038 year

If you want something like CD you can do it in multiple ways (from math side).

Script logic order:
1. Check if storage value is equal or greater then os.time() + 60
2. If is greater or equal then teleport player and set his storage value to os.time() -- this will store time, when he last entered tp
3. If is not greater or equal, then return "you cannot enter more then once per min"
 
Code:
local lastTime = player:getStorageValue(56148)

local now = os.time()

-- If lastTime is -1 or 0 or any other low number, timePassed will be a long number rather than the exact time passed
-- This means we can safely check if timePassed > x even if lastTime was 0 or -1
local timePassed = now - lastTime

if timePassed >= 60 then
        player:setStorageValue(56148, now)
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'You may enter this tp again in 1 minute.')
else
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'It is less then one minute you have entered the tp.')
end


Or we could do it your way and add time directly to storage (no big difference, but I prefer above way):
Code:
local lastTime = player:getStorageValue(56148)

local now = os.time()

-- If lastTime is -1 or 0 or any other low number, timePassed will be a long number rather than the exact time passed
-- This means we can safely check if timePassed > x even if lastTime was 0 or -1
local timePassed = now - lastTime

if timePassed >= 0 then
        player:setStorageValue(56148, now + 60)
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'You may enter this tp again in 1 minute.')
else
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'It is less then one minute you have entered the tp.')
end
 
Last edited:
Solution
Code:
local lastTime = player:getStorageValue(56148)

local now = os.time()

-- If lastTime is -1 or 0 or any other low number, timePassed will be a long number rather than the exact time passed
-- This means we can safely check if timePassed > x even if lastTime was 0 or -1
local timePassed = now - lastTime

if timePassed >= 60 then
        player:setStorageValue(cid, 56148, now)
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'You may enter this tp again in 1 minute.')
else
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'It is less then one minute you have entered the tp.')
end


Or we could do it your way and add time directly to storage (no big difference, but I prefer above way):
Code:
local lastTime = player:getStorageValue(56148)

local now = os.time()

-- If lastTime is -1 or 0 or any other low number, timePassed will be a long number rather than the exact time passed
-- This means we can safely check if timePassed > x even if lastTime was 0 or -1
local timePassed = now - lastTime

if timePassed >= 0 then
        player:setStorageValue(cid, 56148, now + 60)
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'You may enter this tp again in 1 minute.')
else
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'It is less then one minute you have entered the tp.')
end
mm cid is not an argument for player methods anymore
 
Back
Top