Advertisement
chesiren

CC: TicksToSeconds Function

Jan 29th, 2023
753
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.70 KB | Source Code | 0 0
  1. -- simple function so you don't have to do the conversion from ticks to seconds when using computercraft
  2.  
  3. function TicksToSeconds(s)
  4.     s = string.lower(s)
  5.     local endOfString = string.sub(s, -1)
  6.     local number = string.sub(s, 1, -2)
  7.  
  8.     if (endOfString == "s") then
  9.         return tonumber(number)
  10.     elseif (endOfString == "t") then
  11.         return ( tonumber(number) * 0.05 ) --one tick happens every 0.05 seconds
  12.     else
  13.         error("inputed string is not seconds nor ticks")
  14.     end
  15. end
  16.  
  17. -- examples
  18. print(TicksToSeconds("15t")) -- 15 ticks is 0.75 seconds
  19. print(TicksToSeconds("1s"))  -- 1 second is still 1 second
  20. print(TicksToSeconds("1a"))  -- wrong format will give an error
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement