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

Getting a nil value using dofile

Started by _removed, 17 November 2015 - 06:04 PM
_removed #1
Posted 17 November 2015 - 07:04 PM
Been a while :P/>.

But, while making a program, i have been using dofile to access files. Here is an example of my API code:

Note that the API file name is API.lua

function lol()
  return lol
end
return API

This file is called program.lua

api = dofile("API.lua")
print(api.lol())

I get the error on program.lua, and its line 2, attempt to call nil ( a nil value ), which means that it is not a valid function that exists. Can you help me?

- Smigger22

EDIT: Solved.
Edited on 17 November 2015 - 06:13 PM
Creator #2
Posted 17 November 2015 - 07:46 PM

api = {}
function api.lol()
  return "lol"
end
return api
Lupus590 #3
Posted 17 November 2015 - 09:39 PM
To clarify Creator's post, replace the content of API.lua with his code.

You may want to look at and/or use the os.loadAPI function, this may allow you to write a function which will load an API as you tried in you OP.
Edited on 17 November 2015 - 08:41 PM
Creator #4
Posted 17 November 2015 - 09:50 PM
To clarify Creator's post, replace the content of API.lua with his code.

You may want to look at and/or use the os.loadAPI function, this may allow you to write a function which will load an API as you tried in you OP.

smigger22 has been on the forums for a long time, so I thought that this would be self-explanatory.
Lupus590 #5
Posted 18 November 2015 - 12:03 PM
yes, but it's quite possible that other people will read this in future
Edited on 18 November 2015 - 11:03 AM
Creator #6
Posted 18 November 2015 - 01:55 PM
yes, but it's quite possible that other people will read this in future

You are kind of right