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

Get "attempt to call nil" but it works sometimes..?

Started by lorddave, 14 April 2015 - 01:38 PM
lorddave #1
Posted 14 April 2015 - 03:38 PM
Hey coders!

I need some help narrowing down a problem i have with a program that i have written.

http://pastebin.com/d8VkATW3

Everytime the startup program runs after a server restart this error shows up:

startup:13: attempt to call nil

When i comment out that line of code the same happens to the line after that, and after that. until all those four lines are. (the "round(math.floor)" lines)
Then the program starts. The four variables is needed in the program so it wont run well with them commented out.
If i now uncomment those lines the program starts perfectly and everything works.

Why??

Thanks in advance!
Lyqyd #2
Posted 14 April 2015 - 05:43 PM
Because you define the round function after trying to use it for the first time. Since you define it globally, it's in scope the second time you run the program. You should localize your functions and declare them before you try to use them.
Creator #3
Posted 14 April 2015 - 07:02 PM
And you use turbine, which is not defined anywhere, or at least not in the beggining
valithor #4
Posted 14 April 2015 - 07:38 PM
And you use turbine, which is not defined anywhere, or at least not in the beggining

Turbine is defined on line 12 one line above where he uses it. The problem is as lyqyd said using the function before it is defined.