• 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 Countdown til' time

Mjmackan

Mapper ~ Writer
Premium User
Joined
Jul 18, 2009
Messages
1,424
Solutions
15
Reaction score
177
Location
Sweden
Yello, as topic says, how would you in lua countdown to a certain time.
I have an event running in different times, as for example: 20:30
How would i be able to simply check how many seconds there is until 20:30 from now?
 
Solution
Anyone able to help out? This is getting me crazy, I even ran the lines provided in an online compiler and it gave same result.
For some reason it adds 1 hour to the countdown using the lines below:
Lua:
local test = os.time{year = os.date("%Y"), month = os.date("%m"), day = os.date("%d"), hour = 20, min = 30, sec = 0}
print(os.date("%X", test - os.time()))

Edit: Solved by using seconds rather and then converting the seconds using these lines:
Lua:
local rouletteTime = os.time{year = os.date("%Y"), month = os.date("%m"), day = os.date("%d"), hour = showSplit[1], min = showSplit[2], sec = 0}
        rouletteTime = rouletteTime - os.time()
        local hours = string.format("%02.f", math.floor(rouletteTime / 3600))
        local mins =...
There might be a better or easier solution to this (this just came to my mind)
Lua:
local test = os.time{year = os.date("%Y"), month = os.date("%m"), day = os.date("%d"), hour = 20, min = 30, sec = 0}
print(os.date("%X", test - os.time()))
this will print time remaining like "12:30:20".

If you really want it in seconds, you can do it like this:
Lua:
local test = os.time{year = os.date("%Y"), month = os.date("%m"), day = os.date("%d"), hour = 20, min = 30, sec = 0}
print(test - os.time())
 
There might be a better or easier solution to this (this just came to my mind)
Lua:
local test = os.time{year = os.date("%Y"), month = os.date("%m"), day = os.date("%d"), hour = 20, min = 30, sec = 0}
print(os.date("%X", test - os.time()))
this will print time remaining like "12:30:20".

If you really want it in seconds, you can do it like this:
Lua:
local test = os.time{year = os.date("%Y"), month = os.date("%m"), day = os.date("%d"), hour = 20, min = 30, sec = 0}
print(test - os.time())
Thats great, didn't have a clue you could do it like that. However how would I insert the table of times I already have? As shown below 'showClock' is fetching the important time of when the next round of roulette will be played.

This is how the script looks so far, there might be several ways to improve what I've done here.
Lua:
function onThink()
   function getRealTime()
      local hours = tonumber(os.date("%H", os.time()))
      local minutes = tonumber(os.date("%M", os.time()))
      local seconds = tonumber(os.date("%S", os.time()))

      if hours < 10 then
         hours = '0' .. hours
      end
      if minutes < 10 then
         minutes = '0' .. minutes
      end
      if seconds < 10 then
         seconds = '0' .. seconds
      end
      return hours .. ':' .. minutes .. ':' .. seconds
   end

   local cnfPlayTime = { --List of times when roulette should be played.
      {"00:00"},
      {"00:30"},
      {"02:00"},
      {"03:40"},
      {"05:10"},
      {"09:00"},
      {"10:20"},
      {"12:00"},
      {"15:40"},
      {"17:30"},
      {"19:00"},
      {"21:20"},
      {"22:00"},
   }

   function outMessage(showClock)
      local test = os.time{year = os.date("%Y"), month = os.date("%m"), day = os.date("%d"), hour = 20, min = 30, sec = 0}
      print(os.date("%X", test - os.time()))

      local spectators = Game.getSpectators({x = 1086, y = 1228, z = 7}, false, true, 10, 10, 10, 10) --Send message of time left until next round of roulette.
      if #spectators > 0 then
         for i = 1, #spectators do
            spectators[i]:say("Roulette run, is: " .. test, TALKTYPE_MONSTER_SAY, false, spectators[i], {x = 1086, y = 1228, z = 7})
         end
      end
   end

   local showClock = nil
   for i = 1, #cnfPlayTime do
      if getRealTime() >= cnfPlayTime[i][1] then -- checks if time in list selected has passed.
      else
         showClock = cnfPlayTime[i][1] --if not then save it and post it to outMessage.
         outMessage(showClock)
         return true
      end
   end
   return true
end


Edit: I found the command needed, this is how it looks now but for some weird reason it fails with 1 hour. When it should say 7 minutes left for example it shows 1hour 7 minutes instead.
Also i noticed my way to check next time roulette runs is very poor, since it won't countdown to 00:00 if the clock is 23:59.
Lua:
function onThink()
   function getRealTime()
      local hours = tonumber(os.date("%H", os.time()))
      local minutes = tonumber(os.date("%M", os.time()))
      local seconds = tonumber(os.date("%S", os.time()))

      if hours < 10 then
         hours = '0' .. hours
      end
      if minutes < 10 then
         minutes = '0' .. minutes
      end
      if seconds < 10 then
         seconds = '0' .. seconds
      end
      return hours .. ':' .. minutes .. ':' .. seconds
   end

   local cnfPlayTime = { --List of times when roulette should be played.
      {"00:00"},
      {"00:30"},
      {"02:00"},
      {"03:40"},
      {"05:10"},
      {"09:00"},
      {"10:20"},
      {"12:00"},
      {"15:40"},
      {"17:30"},
      {"19:00"},
      {"21:20"},
      {"22:00"},
   }

   function addReward()
      local players = Game.getPlayers()
      for _, player in pairs(players) do
         local winningNumber = math.random(0,35)
         if player:getStorageValue(72450) == 1 then
            for u = 0,11 do
               local stor = u*10000
               if player:getStorageValue(7245 + stor) >= 0 then

                  if player:getStorageValue(7245 + stor) == winningNumber then
                  end

               end
            end
         end
      end
   end

   function outMessage(showClock)
      local showSplit = showClock:splitTrimmed(":")
      local test = os.time{year = os.date("%Y"), month = os.date("%m"), day = os.date("%d"), hour = showSplit[1], min = showSplit[2], sec = 0}

      local spectators = Game.getSpectators({x = 1086, y = 1228, z = 7}, false, true, 10, 10, 10, 10) -- Send message of time left until next round of roulette.
      if #spectators > 0 then
         for i = 1, #spectators do
            spectators[i]:say("Next run in: \n[" .. os.date("%X", test - os.time()) .. "]", TALKTYPE_MONSTER_SAY, false, spectators[i], {x = 1086, y = 1228, z = 7})
         end
      end
   end

   for i = 1, #cnfPlayTime do
      if getRealTime() >= cnfPlayTime[i][1] then -- checks if time in list selected has passed.
      else
         outMessage(cnfPlayTime[i][1])
         return true
      end
   end
   return true
end
 
Last edited:
Anyone able to help out? This is getting me crazy, I even ran the lines provided in an online compiler and it gave same result.
For some reason it adds 1 hour to the countdown using the lines below:
Lua:
local test = os.time{year = os.date("%Y"), month = os.date("%m"), day = os.date("%d"), hour = 20, min = 30, sec = 0}
print(os.date("%X", test - os.time()))

Edit: Solved by using seconds rather and then converting the seconds using these lines:
Lua:
local rouletteTime = os.time{year = os.date("%Y"), month = os.date("%m"), day = os.date("%d"), hour = showSplit[1], min = showSplit[2], sec = 0}
        rouletteTime = rouletteTime - os.time()
        local hours = string.format("%02.f", math.floor(rouletteTime / 3600))
        local mins = string.format("%02.f", math.floor(rouletteTime / 60 - (hours * 60)))
        local secs = string.format("%02.f", math.floor(rouletteTime  - hours * 3600 - mins * 60))
        local time_string = hours ..":".. mins ..":".. secs
 
Last edited:
Solution
Back
Top