Jump to content




Tree farm [string:1: attempt to call nil]

help lua

  • You cannot reply to this topic
6 replies to this topic

#1 jag

  • Members
  • 533 posts
  • LocationStockholm, Sweden

Posted 31 October 2012 - 02:02 AM

So straight forward:
  • I got a tree farm program.
  • It's buggy.
  • Here's the code:
    Spoiler
So the error that I'm getting is:
string:1: attempt to call nil
I've never seen this error before and I can't find any solutions on the interwebz.

EDIT:​ I caught one typo:
-- on line 118
    if not select(2,4,"count") then
-- misspelled count
-- first letter should be uppercase
    if not select(2,4,"Count") then
Now I'm getting another error, and I haven't seen this one either before:
string:1: Expected number


#2 ChunLing

  • Members
  • 2,027 posts

Posted 31 October 2012 - 07:44 AM

This kind of error usually indicates that you have tried to use loadstring on a bit of code that was syntactically valid (so loadstring turned it into a function named "string") but then you ran it and it was missing something.
option = loadstring("turtle.getItem"..option.."(slot)")
Works if you feed it "Space" or "Count" as the option. But at one point you fed it "count"
if not select(2,4,"count") then 
You might have fed it something bad in other places, I'm not sure. But this is kinda a wonky use of loadstring, methinks. You should just say "turtle["getItem"..option](slot)" instead of using loadstring to retype option and then running option here. And not just because loadstring errors are difficult to debug (really you should always pcall functions made by loadstring, I should do that myself). Calls of loadstring require the creation of a whole new virtual environment, they're super inefficient ways to do things that you might do any other way.

#3 jag

  • Members
  • 533 posts
  • LocationStockholm, Sweden

Posted 31 October 2012 - 01:18 PM

View PostChunLing, on 31 October 2012 - 07:44 AM, said:

This kind of error usually indicates that you have tried to use loadstring on a bit of code that was syntactically valid (so loadstring turned it into a function named "string") but then you ran it and it was missing something.
option = loadstring("turtle.getItem"..option.."(slot)")
Works if you feed it "Space" or "Count" as the option. But at one point you fed it "count"
if not select(2,4,"count") then 
You might have fed it something bad in other places, I'm not sure. But this is kinda a wonky use of loadstring, methinks. You should just say "turtle["getItem"..option](slot)" instead of using loadstring to retype option and then running option here. And not just because loadstring errors are difficult to debug (really you should always pcall functions made by loadstring, I should do that myself). Calls of loadstring require the creation of a whole new virtual environment, they're super inefficient ways to do things that you might do any other way.
Ah good idea with the
turtle["getItem"..option](slot)
idea! Thanks!
I'm going to try it now..

#4 ChunLing

  • Members
  • 2,027 posts

Posted 31 October 2012 - 11:03 PM

You still have to feed it correctly, only "Space" and "Count", but at least if it fails you know instantly what failed. And it's much faster execution-wise.

#5 jag

  • Members
  • 533 posts
  • LocationStockholm, Sweden

Posted 31 October 2012 - 11:26 PM

But what if I do
option = option:lower()
option = option:gsub("%a",string.upper(),1)
Wouldn't that work?

#6 ChunLing

  • Members
  • 2,027 posts

Posted 31 October 2012 - 11:42 PM

... :P/>
Just feed it correctly. I mean, yeah, you can use string functions to ensure that user entered strings are more rather than less correct, but for the fixed strings in your code, they should just be right.

You had me going there. :P/>

#7 jag

  • Members
  • 533 posts
  • LocationStockholm, Sweden

Posted 01 November 2012 - 01:01 AM

Thanks for the help btw!





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users