This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Enderslime's profile picture

clock:31: attempt to concatenate nil and string

Started by Enderslime, 14 November 2015 - 02:39 AM
Enderslime #1
Posted 14 November 2015 - 03:39 AM
function printSecondsAsDaysHoursMinutesSeconds(s)

local calculate = 1
local seconds = tonumber(s)

while seconds > 0 do

if calculate == 1 then

local minutes = math.floor(seconds / 60)
local seconds = seconds - minutes * 60

local hours = math.floor(minutes / 60)
local minutesHolder = hours * 60
local minutes = minutes - minutesHolder

local days = math.floor(hours / 24)
local hours = hours - days * 24

local seconds = tonumber(seconds)
local minutes = tonumber(minutes)
local hours = tonumber(hours)
local days = tonumber(days)

local calculate = 0
end
sleep(1)
local seconds = seconds - 1
term.clear()
term.setCursorPos(1,1)
print(days..":"..hours..":"..minutes..":"..seconds) < – line 31
if seconds == 0 and minutes > 0 then
local seconds = seconds + 59
local minutes = minutes - 1
elseif minutes == 0 and hours > 0 then
local minutes = minutes + 59
local hours = hours - 1
elseif hours == 0 and days > 0 then
local hours = hours + 24
local days = days - 1
elseif days == 0 and hours == 0 and minutes == 0 and seconds == 0 then
term.clear()
term.setCursorPos(1,1)
print("Countdown Complete!")
sleep(3)

end
end
end
print("Seconds?")
local seconds = read()
printSecondsAsDaysHoursMinutesSeconds(seconds)

its saying im trying to concatenate nil and string, even when ive tonumbered all the numbers.
hbomb79 #2
Posted 14 November 2015 - 06:08 AM
When I run your code days, hours and minutes are all nil. The reason being all the variables are defined as 'local' inside of an IF statement. Making them inaccessible to any code outside of it.


Also, in future it will make it a lot easier for people on the forums to view your code if you use the code wrapping when pasting code.
Edited on 14 November 2015 - 05:14 AM
valithor #3
Posted 14 November 2015 - 03:13 PM
Should also be noted that you only use the local keyword the first time you declare the variable, not every time you change what it is equal to:

Example:

local variable = "hello"
variable = "bye"