Hello, I am making a In Game clock with lamps and computer.
What i want to know is how i could split a variable as the time.
for the minutes computer i want to take the minutes number.
Exemple:In minecraft it is 12:47 PM
I want to have the number 7 alone.
then number 4 for the second computer
etc for 2 and 1.
I know how to get the time but not how to split the variable into 4 variables.
Thanks!
Take a look at string.sub("string", startN, endN)
example:
string.sub("Hello", 1, 1) --> "H"
string.sub("Hello", 3, 3) --> "l"
So, for you: (assuming os.time() always returns 1.234 (numbers change, period stays in place)
string.sub(os.time(), 1, 1) -> Number 1
-- We don't want the 2nd character, since it's just a period.
string.sub(os.time(), 3, 3) -> Number 2
string.sub(os.time(), 4, 4) -> Number 3
string.sub(os.time(), 5, 5) -> Number 4
Above is wrong, but you can still fix it yourself (os.time returns 1-24.000-999) use it to check and fix. I'll post a fixed version in a few minutes.
Fixed version:
local function getDigits()
local digits = {0, 0, 0, 0} -- Sets digits[1-4] at default of 0, not nil
local time = tostring(os.time())
if time:byte(2) == 46 then
-- ^ This is essentially just a shortcut to 'if time:sub(2,2) == "." then'
digits[2] = tonumber(time:sub(1,1))
digits[3] = tonumber(time:sub(3,3))
digits[4] = tonumber(time:sub(4,4))
else
digits[1] = tonumber(time:sub(1,1))
digits[2] = tonumber(time:sub(2,2))
digits[3] = tonumber(time:sub(4,4))
digits[4] = tonumber(time:sub(5,5))
end
return digits
end
print(textutils.tabulate(getDigits())) -- You can remove this line, it was for debugs.
getDigits() will return the digits of the clock, in a table with 4 numbers.
So, if you have computer1, which will display the first hour digit: use getDigits()[1] – make sure that the above function is defined BEFORE you call getDigits(), or you'll error.