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

function problem - Fixed

Started by Alex_, 18 March 2012 - 07:50 PM
Alex_ #1
Posted 18 March 2012 - 08:50 PM
Old Code
– Copyright Alexander Bolton © 2012-
print("Starting CraftOs Server Program By Alex_")
print("which side is the modem?")
modem=read()
print("Admin password for session")
apass=read()
fs.delete("users/admin")
fs.makeDir("users/admin")
fs.makeDir("users/admin/" .. apass)
fs.makeDir("users/admin/privalege")
fs.makeDir("users/admin/privalege/superuser")
print("password set with privalege superuser")
sleep(1)
rednet.open(modem)
term.clear()
term.setCursorPos(1,1)
print("Server Started")
print("Click CTRL For Console")
print("===============================================")
while true do
print("scanning")
from,rp1=rednet.receive(60)
sleep(1)
from,rp2=rednet.receive(60)
sleep(1)
from,rp3=rednet.receive(60)
sleep(1)
from,rp4=rednet.receive(60)
sleep(1)
if rp1=="login" then main() – start:30: attempt to call nil
end
end
function main()
if fs.exists("users/" .. rp2 .. "/" .. rp3) then
if fs.exists("users/" .. rp3 .. "/privalege/superuser") then
print(rp2, " found. sending admin login codes")
rednet.broadcast(rp1)
sleep(2)
rednet.broadcast(rp2)
sleep(2)
rednet.broadcast(rp4)
sleep(2)
rednet.broadcast("superuser")
print(rp2, ":", rp4, ":", "superuser:has logged in")
elseif fs.exists("users/" .. rp2 .. "/" .. rp3) then
print(rp2, " found. sending norm login codes")
rednet.broadcast(rp1)
sleep(2)
rednet.broadcast(rp2)
sleep(2)
rednet.broadcast(rp4)
sleep(2)
rednet.broadcast("norm")
print(rp2, ":", rp4, ":", "norm:has logged in")
end
end
function ashutdown()
print("shutting down")
sleep(2)
os.shutdown()
end
end

im getting a problem with the function but can't find it

it says attempt to call nil but the function "login()" exists!

edit:
I moved code to top and it didn't work at all it just broke the code

new code still not working
http://pastebin.com/2Ybdi3kg

Edit Fixed i left 2 ends at bottom
Thanks Lirall
Jajnick #2
Posted 18 March 2012 - 09:10 PM
Umm… you first declare the function, not call it

Also, did you mean function "main()"?
Liraal #3
Posted 18 March 2012 - 09:32 PM
i won't help with a copyrighted program :D/>/>

just kidding, move the main() to the top of the code and it should work.
Alex_ #4
Posted 18 March 2012 - 09:38 PM
i won't help with a copyrighted program :D/>/>

just kidding, move the main() to the top of the code and it should work.
sorry the copyright was a joke to my friends helping me on this! LOL :)/>/>
Alex_ #5
Posted 18 March 2012 - 09:40 PM
Umm… you first declare the function, not call it

Also, did you mean function "main()"?
yep
Alex_ #6
Posted 18 March 2012 - 09:42 PM
i won't help with a copyrighted program :D/>/>

just kidding, move the main() to the top of the code and it should work.
i just did that and it didn't work
Jajnick #7
Posted 18 March 2012 - 09:45 PM
What?
How could it don't work, post your code…
Liraal #8
Posted 18 March 2012 - 09:55 PM
okay, can you paste the new code and the error message?
Luanub #9
Posted 18 March 2012 - 11:05 PM
Move the function to the top of the code.

Put everything that is going to execute when the code starts below the function including the call to it.

so

function blah()
code
end

print ("welcome")
code
blah()

Sorry I see this is how you broke it… It is the proper method so it should of fixed your problem.

What do you use to code with? I would suggesting tossing this in notepad++ or something like that and indenting the code. Might help to find your problem a little faster.
Alex_ #10
Posted 19 March 2012 - 11:06 AM
What?
How could it don't work, post your code…
I will when i get home currently stuck in school!
ICT Lessons are boring at the moment!
:D/>/>
Alex_ #11
Posted 19 March 2012 - 03:07 PM
okay, can you paste the new code and the error message?
http://pastebin.com/2Ybdi3kg
^^ thats my new code
I don't get an error message the program won't even show the print it just!
Liraal #12
Posted 19 March 2012 - 03:10 PM
oh i know
the main() function is actually unnecessary in lua and it doesn't get called automatically.you should just drop it and delete 'function main()' line and the last 'end'
Alex_ #13
Posted 19 March 2012 - 03:15 PM
oh i know
the main() function is actually unnecessary in lua and it doesn't get called automatically.you should just drop it and delete 'function main()' line and the last 'end'
Yep but this is just one command out of many on my old old old code thats why i want it in functions. The main was simply the login command previously function login() then there is other command like register and change … You get what i mean i don't want a bunch of else if's
Liraal #14
Posted 19 March 2012 - 03:17 PM
then you did the moving up wrong and left two 'end's at the bottom. lift them before the ashutdown function and then try
Alex_ #15
Posted 19 March 2012 - 03:22 PM
then you did the moving up wrong and left two 'end's at the bottom. lift them before the ashutdown function and then try
Thanks that worked