Lua os time () storage set for a time

Discussion in 'Support' started by ellvo, Jan 11, 2017.

  1. ellvo

    ellvo New Member

    Joined:
    Sep 23, 2016
    Messages:
    11
    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: Jan 12, 2017
  2. Xeraphus

    Xeraphus the best

    Joined:
    Feb 14, 2015
    Messages:
    1,723
    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
     
  3. Taurus

    Taurus Texass

    Joined:
    Jan 11, 2009
    Messages:
    396
    +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.
     
  4. ellvo

    ellvo New Member

    Joined:
    Sep 23, 2016
    Messages:
    11
    bump
     
  5. andu

    andu Sold 649 scripts, 25 maps and 9 events!

    Joined:
    Aug 7, 2009
    Messages:
    786
    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"
     
  6. Colandus

    Colandus Well-Known Member

    Joined:
    Jun 6, 2007
    Messages:
    2,291
    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: Jan 12, 2017
  7. Xeraphus

    Xeraphus the best

    Joined:
    Feb 14, 2015
    Messages:
    1,723
    mm cid is not an argument for player methods anymore
     
  8. Colandus

    Colandus Well-Known Member

    Joined:
    Jun 6, 2007
    Messages:
    2,291
    Yeah didn't check it, just copied his code
     
    Xeraphus likes this.

Share This Page