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

No Errors But Stops Anyway?

Started by HellerTech, 15 March 2015 - 09:14 PM
HellerTech #1
Posted 15 March 2015 - 10:14 PM
I started developing this program which is meant to be used in conjunction with Tinker's Construct. It is to get how many blocks the user wants, and run the code inside the "for" loop as many times as the amount of block specified. This works flawlessly in a simple "while" loop, but when used in a "for" loop it only runs the cycle once and then exits the program after a short wait. There are no errors displayed when the program terminates. Why is this? Is it structured wrong? Please give me some help/advice :)/> I'm new to lua and having some issues. Thank You – Heller



-- Used to cast specified amount of blocks , using The mod Tinker's Construct

local times = 0
term.write("How many cycles to run: ")
time = read()

function clean() -- Clears screen and sets cursor to default position
  term.clear()
  term.setCursorPos(1,1)
end
function pour() -- Main code to turn redstone signal on and off
    print("Pouring")
    rs.setOutput("bottom", true)
    sleep(13)
    print("Cooling")
    rs.setOutput("bottom", false)
    sleep(5)
    print("Casted")
sleep(1)
end
for i = 0, times do -- Meant to be official code to carry out process
pour()
clean()
end
valithor #2
Posted 16 March 2015 - 03:10 AM
in your for loop you are doing the loop until i equals times. Times is 0 and you never change that so the for loop never loops. I am assuming you want to do something like

time = read()
times = tonumber(time)
or alternatively

times = tonumber(read()) or 0

Just so you know read() returns a string, and so you have to use tonumber on it to use it as a number. tonumber will return either a number if it can be converted to a number or nil.
Edited on 16 March 2015 - 02:13 AM
HellerTech #3
Posted 16 March 2015 - 04:48 AM
Thank you :DD Only have one problem which I found a work around, It makes one more block than user inputs. I solved this by simply putting -1 after the "times" in the "for" statement.
Dragon53535 #4
Posted 16 March 2015 - 01:34 PM
Thank you :DD Only have one problem which I found a work around, It makes one more block than user inputs. I solved this by simply putting -1 after the "times" in the "for" statement.

You do realize that you can start at 1, and solve that problem as well.
HPWebcamAble #5
Posted 16 March 2015 - 03:24 PM
Thank you :DD Only have one problem which I found a work around, It makes one more block than user inputs. I solved this by simply putting -1 after the "times" in the "for" statement.
You do realize that you can start at 1, and solve that problem as well.


for i = 1, times do --# Executes 'times'

for i = 0, times do --# Executes 'times' + 1