Jump to content

  • Curse Sites
Become a Premium Member! Help
Latest News Article

Computercraft Codes help


  • Please log in to reply
18 replies to this topic

#1

MrHedmad
    MrHedmad

    Out of the Water

  • Members
  • 8 posts
  • Location: Italy
  • Minecraft: LucV996

Posted 18 February 2012 - 05:07 PM

Hi!


I just started to code some programs whit computercraft's lua. I have a lot of small errors and bits of wrong coding so i've got an idea! Why i don't ask the others? so i started this: Please post any computercraft code-related issues down here!

Note: Anyone can post a code to check. I'll do the possible on checking it and the others can test it too. if you post a code down here, don't get upset if someone "steal" it and uses it.

Note 2.0: Anyone can help checking code. It helps a lot when learning how to code.

Also there is a great tutorial on how to code in lua at this [LINK]

Hope it helps.

(P.S: if you find any errors on my spelling let me know)
Posted Image

Register or log in to remove.

#2

nwalker248
  • Location: England
  • Minecraft: nwalker248

Posted 18 February 2012 - 05:08 PM

Im sure youd get better help on the computercraft page...
Creating Artwork, Banners, And Avatars. Check My "About Me" For More Information!!!! Have i helped you?? Press the    Posted Image !!!

#3

MrHedmad
    MrHedmad

    Out of the Water

  • Members
  • 8 posts
  • Location: Italy
  • Minecraft: LucV996

Posted 18 February 2012 - 05:25 PM

View Postnwalker248, on 18 February 2012 - 05:08 PM, said:

Im sure youd get better help on the computercraft page...

Well i don't whant to register in a thousand of forums and i don't whant to spam the mod page.

So, let's start!

Here's my code:

Spoiler:
I get some errors so can you help? If you have the time to read it all you can also learn something (maybe)!
Posted Image

#4

Sorfak
    Sorfak

    Carpenter

  • Members
  • 47 posts

Posted 25 February 2012 - 08:24 PM

the third time you wrote term.SetCursorPos you forgot the "m"
EDIT: i might be wrong, but don't you have to type () ofter term.clear?
Posted Image

#5

duncan999007

Posted 26 February 2012 - 04:31 AM

EDIT: nvm...

#6

ilua360
    ilua360

    Out of the Water

  • Members
  • 3 posts

Posted 26 February 2012 - 10:59 AM

  • term.clear is a function and should be called with the proper function call syntax (term.clear)
  • A, B, C and D are being set to nil, I assume you should be setting them to string values and therefore should surround the values in quotation marks.
  • These should also be local variables.
  • print takes a string argument, strings should be surrounded with qutation marks.
  • Most variables should be declared as local as they are only needed in this file (or function).
  • You need to escape quotation marks inside strings (ie "\"" insteqad of """)
  • Some of your if statements were missing the 'then' keyword.

Spoiler:

By no means am I saying that this code will now work, I have no experience with computercraft; I have merely corrected the syntax. (I may have missed one or two things).

#7

Stirfry_Noodles

Posted 29 February 2012 - 05:24 AM

I'm not sure about it but, are you writing input as "imput", if this is an error than that could be causing half your problems. And the above post is correct ins saying that you need to assign "A" to something to make it work.

e.g.

A = "12345"
B = "23456"

etc.


Just adding to what else you did, at the top you should probable have a simpler system of selecting the option:

print("[1] To access the reactor room.")
print("[2] To send a uranium cell to the reactor.")
print("[3] To stop the reactor and turn on the safety protocol")
write("What do you want to do? : ")

A lot of errors have been made in the typing itself and this causes problems because the computer then doesn't know what do.

I hope this has been helpful!

Regards; StirfryNoodles


BTW.
I am currently working a new program which allows the control of multiple systems on a computer. e.g. the lighting <REDPOWER>, IC2 ad Buildcraft machines, as well as piston doors and even a self destruct capability.
On top of that i'm making it so that you have to "login" with an account and it's associated password in order to get to the menu. And for more serious stuff, like the self destruct, an extra set of Arming codes. However this involves a lot of work because it means creating a lot of if / else statements.

If anyone can give me advice about this program then please do!

#8

ilua360
    ilua360

    Out of the Water

  • Members
  • 3 posts

Posted 29 February 2012 - 07:37 AM

View PostStirfry_Noodles, on 29 February 2012 - 05:24 AM, said:

I'm not sure about it but, are you writing input as "imput", if this is an error than that could be causing half your problems.

This isn't an error because imput is a user defined variable, it doesn't matter if he calls it 'input', 'imput' or whatever as long as the spelling is consistent throughout.

#9

lillenthaman
  • Location: Sweden
  • Minecraft: lillenthaman

Posted 13 July 2012 - 09:44 AM

I got some problem with my code and i cant seem to find the problem, i just recently started useing lua so im not that good at it :S there is 3 programs linked to eachother its for a adventure thingy were u hack into a computer. the program works perfectly fine if u dont type anything wrong while ur useing it, but i cant get the wrong command thingy to work :S would appreciate any help.

heres the code for the first one:
right = "heaps of fail"
debug = "cmd"
outputface = "right"
maxtries = 3
tries = 0
function Lock()
password()
if user == debug then
print "Safe Mode Activated"
sleep(1)
shell.run"clear"
sleep(1)
shell.run"cmd"
end
if user == right then
print "Access Granted! Welcome Back Sir."
correct()
sleep(2)
shell.run"clear"
shell.run"shell"
end
if not(user == right1) or (user == debug) then
if tries == maxtries -1 then
print "ERROR!!!"
print "Shutting Down..."
sleep (2)
os.shutdown()
end
else
print "Access Denied!"
tries = tries+1
aprint ("Tries Left: ", maxtries - tries)
sleep(2)
end
end
function correct()
end
function password()
print "Running Windows Firewall \nSystem is now under Protection!"
write "Password: "
user= read("*")
end
Lock()

and the second one:
right = "Move to Root"
debug = "reboot"
outputface = "right"
function Lock()
password()
if user == debug then
print "Rebooting Computer..."
sleep(2)
shell.run"clear"
os.reboot()
end
if user == right then
correct1()
end
if not(user == right) or (user == debug) then
print "That is not a internal command, external command, program or commandfile"
sleep(1)
write "C:Users:lillen>"
user= read()
end
end
function correct1()
sleep(2)
shell.run"cmd2"
end
function password()
print ("Minecraft Windows [Version 1.2] \nCopyright <c> 2012 Minecraft Corporation.\n")
sleep(1)
write "C:Users:lillen>"
user= read()
end
Lock()

and finaly the third one:
right = "Find Root Password"
debug = "reboot"
outputface = "right"
function Lock()
password()
if user == debug then
print "Rebooting Computer..."
sleep(2)
shell.run"clear"
os.reboot()
end
if user == right then
correct2()
end
if not(user == right1) or (user == debug) then
print "That is not a internal command, external command, program or commandfile"
sleep(1)
write "C:>"
user= read()
end
end
function correct2()
sleep(2)
print "Root Password <heaps of fail>"
sleep(1)
print "Computer Rebooting in 6 seconds..."
sleep(6)
shell.run"clear"
os.reboot()
end
function password()
write "C:>"
user= read()
end
Lock()

Sorry for the long post, dont know how to make spoilers :S
Posted Image
Posted Image
Posted Image

#10

kaimaxmunte
  • Location: Dutch

Posted 17 July 2012 - 07:15 PM

View PostStirfry_Noodles, on 29 February 2012 - 05:24 AM, said:

I'm not sure about it but, are you writing input as "imput", if this is an error than that could be causing half your problems. And the above post is correct ins saying that you need to assign "A" to something to make it work.

e.g.

A = "12345"
B = "23456"

etc.


Just adding to what else you did, at the top you should probable have a simpler system of selecting the option:

print("[1] To access the reactor room.")
print("[2] To send a uranium cell to the reactor.")
print("[3] To stop the reactor and turn on the safety protocol")
write("What do you want to do? : ")

A lot of errors have been made in the typing itself and this causes problems because the computer then doesn't know what do.

I hope this has been helpful!

Regards; StirfryNoodles


BTW.
I am currently working a new program which allows the control of multiple systems on a computer. e.g. the lighting <REDPOWER>, IC2 ad Buildcraft machines, as well as piston doors and even a self destruct capability.
On top of that i'm making it so that you have to "login" with an account and it's associated password in order to get to the menu. And for more serious stuff, like the self destruct, an extra set of Arming codes. However this involves a lot of work because it means creating a lot of if / else statements.

If anyone can give me advice about this program then please do!
um you seem verry pro to me in this could you help fixing this becouse this is exactly what i need i got into the computercraft folder from my server and then \lua\rom\programs i copyed there a program and renamed it to reactor then i replaced the code with this code from above
term.clear
term.setCursorPos (1,1)
A=OpenReactorRoom
B=RefillReactor
C=EmergencyReactorStop
D=Done
Print("Reactor room safety protocol 0.01 running...")
sleep(2)
print("Welcome to the RRSP V.0.01")
sleep(1)
Print("What do you whis to do?")
sleep(1)
print("Spell "OpenReactorRoom" To access the reactor room.")
print("Spell "RefillReactor" to send a uranium cell to the reactor.")
print("Spell "EmergencyReactorStop" to stop the reactor and turn on the safety protocol")
write("What do you whant to do? : ")
input = read()
if input == A then
term.clear
term.setCursorPos(1,1)
	print("Opening the reactor room...")
	rs.setOutput("left",true)
	sleep(1)
term.clear
ter.setCursorPos (1,1)
	print("Door open. When you have done, spell "Done" and the door will close")
	write("What do you whant to do? : ")
	secondinput = read()
	if secondinput == D then
term.clear
	term.setCursorPos (1,1)
print("Closing the door...")
sleep(1)
rs.setOutput("left",false)
print("thank you for using the RRSP V. 0.01")
os.shutdown()
else
term.clear
term.setCursorPos (1,1)
print("Command incorrect, shutting down.")
rs.setOutput("right",false)
rs.setOutput("left",false)
rs.setOutput("up",false)
rs.setOutput("down",false)
rs.setOutput("back",false)
sleep(2)
os.shutdown()
end
	else
	term.clear
	term.setCursorPos (1,1)
	print("Command incorrect, shutting down.")
	rs.setOutput("right",false)
	rs.setOutput("left",false)
	rs.setOutput("up",false)
	rs.setOutput("down",false)
	rs.setOutput("back",false)
	sleep(2)
	os.shutdown()
elseif input == B
	term.clear
term.setCursorPos (1,1)
	print (Refilling the reactor whit a new uranium cell..)
	rs.setOutput("right",true)
	sleep (1)
	rs.setOutput("right",false)
	print ("the cell is entering the reactor. Tank you for using the RRSP V 0.01")
	sleep (2)
	os.shutdown()
elseif input == C
term.clear
term.setCursorPos (1,1)
	print ("Shutting down the reactor...)
	rs.setOutput("up",true)
	sleep (1)
	term.clear
	term.setCursorPos (1,1)
	print ("The reactor is turned off. When you whis to turn it back on, spell "Done"")
	write ("What do you whant to do? : ")
	tirdinput = read()
	if tirdinput == D
term.clear
term.setCursorPos (1,1)
print ("Turinng the reactor on again, please stand by...")
rs.setOutput("up",false)
sleep(2)
term.clear
term.setCursorPos (1,1)
else
term.clear
term.setCursorPos (1,1)
print("Command incorrect, shutting down.")
rs.setOutput("right",false)
rs.setOutput("left",false)
rs.setOutput("up",false)
rs.setOutput("down",false)
rs.setOutput("back",false)
sleep (2)
os.shutdown()
end
else
term.clear
term.setCursorPos (1,1)
print("Command incorrect, shutting down.")
rs.setOutput("right",false)
rs.setOutput("left",false)
rs.setOutput("up",false)
rs.setOutput("down",false)
rs.setOutput("back",false)
sleep (2)
os.shutdown()
end
bios:206: [string "reactor"]:2:  '=' expected

and then ingame it shows the program named reactor but when i start it it says

#11

Monkeydash

Posted 26 September 2012 - 02:16 PM

View Postkaimaxmunte, on 17 July 2012 - 07:15 PM, said:

um you seem verry pro to me in this could you help fixing this becouse this is exactly what i need i got into the computercraft folder from my server and then \lua\rom\programs i copyed there a program and renamed it to reactor then i replaced the code with this code from above
term.clear
term.setCursorPos (1,1)
A=OpenReactorRoom
B=RefillReactor
C=EmergencyReactorStop
D=Done
Print("Reactor room safety protocol 0.01 running...")
sleep(2)
print("Welcome to the RRSP V.0.01")
sleep(1)
Print("What do you whis to do?")
sleep(1)
print("Spell "OpenReactorRoom" To access the reactor room.")
print("Spell "RefillReactor" to send a uranium cell to the reactor.")
print("Spell "EmergencyReactorStop" to stop the reactor and turn on the safety protocol")
write("What do you whant to do? : ")
input = read()
if input == A then
term.clear
term.setCursorPos(1,1)
print("Opening the reactor room...")
rs.setOutput("left",true)
sleep(1)
term.clear
ter.setCursorPos (1,1)
print("Door open. When you have done, spell "Done" and the door will close")
write("What do you whant to do? : ")
secondinput = read()
if secondinput == D then
term.clear
term.setCursorPos (1,1)
print("Closing the door...")
sleep(1)
rs.setOutput("left",false)
print("thank you for using the RRSP V. 0.01")
os.shutdown()
else
term.clear
term.setCursorPos (1,1)
print("Command incorrect, shutting down.")
rs.setOutput("right",false)
rs.setOutput("left",false)
rs.setOutput("up",false)
rs.setOutput("down",false)
rs.setOutput("back",false)
sleep(2)
os.shutdown()
end
else
term.clear
term.setCursorPos (1,1)
print("Command incorrect, shutting down.")
rs.setOutput("right",false)
rs.setOutput("left",false)
rs.setOutput("up",false)
rs.setOutput("down",false)
rs.setOutput("back",false)
sleep(2)
os.shutdown()
elseif input == B
term.clear
term.setCursorPos (1,1)
print (Refilling the reactor whit a new uranium cell..)
rs.setOutput("right",true)
sleep (1)
rs.setOutput("right",false)
print ("the cell is entering the reactor. Tank you for using the RRSP V 0.01")
sleep (2)
os.shutdown()
elseif input == C
term.clear
term.setCursorPos (1,1)
print ("Shutting down the reactor...)
rs.setOutput("up",true)
sleep (1)
term.clear
term.setCursorPos (1,1)
print ("The reactor is turned off. When you whis to turn it back on, spell "Done"")
write ("What do you whant to do? : ")
tirdinput = read()
if tirdinput == D
term.clear
term.setCursorPos (1,1)
print ("Turinng the reactor on again, please stand by...")
rs.setOutput("up",false)
sleep(2)
term.clear
term.setCursorPos (1,1)
else
term.clear
term.setCursorPos (1,1)
print("Command incorrect, shutting down.")
rs.setOutput("right",false)
rs.setOutput("left",false)
rs.setOutput("up",false)
rs.setOutput("down",false)
rs.setOutput("back",false)
sleep (2)
os.shutdown()
end
else
term.clear
term.setCursorPos (1,1)
print("Command incorrect, shutting down.")
rs.setOutput("right",false)
rs.setOutput("left",false)
rs.setOutput("up",false)
rs.setOutput("down",false)
rs.setOutput("back",false)
sleep (2)
os.shutdown()
end
bios:206: [string "reactor"]:2:  '=' expected

and then ingame it shows the program named reactor but when i start it it says

I have remade your code and as long as I can see it now works as intended! Here is the new code:
---- Variables -----
A = "OpenReactorRoom"
B = "RefillReactor"
C = "EmergencyReactorStop"
D = "Done"
-----Code part ---
term.clear()
term.setCursorPos(1,1)
print("Reactor room safety protocoll 0.01 running...")
sleep(2)
print("Welcome to the RRSP V.0.01")
sleep(1)
print("What do you wish to do?")
sleep(1)
print("Spell \"OpenReactorRoom\" To access the reactor room.")
print("Spell \"RefillReactor\" to send a uranium cell to the reactor.")
print("Spell \"EmergencyReactorStop\" to stop the reactor and turn on the safety protocol")
write("What do you want to do? : ")
input = read()
if input == A then
term.clear()
term.setCursorPos(1,1)
print("Opening the reactor room...")
rs.setOutput("left", true)
sleep(1)
term.clear()
term.setCursorPos(1,1)
print("Door open. When you are done, spell \"Done\" and the door will close")
write("What do you whant to do? : ")
secondinput = read()
if secondinput == D then
  term.clear()
  term.setCursorPos (1,1)
  print("Closing the door...")
  sleep(1)
  rs.setOutput("left", false)
  print("thank you for using the RRSP V. 0.01")
  sleep(2)
  os.shutdown()
else
  term.clear()
  term.setCursorPos(1,1)
  print("Command incorrect, shutting down.")
  rs.setOutput("right", false)
  rs.setOutput("left", false)
  rs.setOutput("top", false)
  rs.setOutput("bottom", false)
  rs.setOutput("back", false)
  sleep(2)
  os.shutdown()
  end  
--[[
else
term.clear()
term.setCursorPos(1,1)
print("Command incorrect, shutting down.")
rs.setOutput("right", false)
rs.setOutput("left", false)
rs.setOutput("top", false)
rs.setOutput("bottom", false)
rs.setOutput("back", false)
sleep(2)
os.shutdown()
--]]
elseif input == B then
term.clear()
term.setCursorPos(1,1)
print("Refilling the reactor with a new uranium cell..")
rs.setOutput("right", true)
sleep(1)
rs.setOutput("right", false)
print("the cell is entering the reactor. Thank you for using the RRSP V 0.01")
sleep(2)
os.shutdown()
elseif input == C then
term.clear()
term.setCursorPos (1,1)
print ("Shutting down the reactor...")
rs.setOutput("top", true)
sleep (1)
term.clear()
term.setCursorPos(1,1)
print("The reactor is turned off. When you wish to turn it back on, spell \"Done\"")
write ("What do you want to do? : ")
thirdinput = read()
if thirdinput == D then
  term.clear()
  term.setCursorPos(1,1)
  print ("Turinng the reactor on again, please stand by...")
  rs.setOutput("top", false)
  sleep(2)
  term.clear()
  term.setCursorPos(1,1)
else
  term.clear()
  term.setCursorPos(1,1)
  print("Command incorrect, shutting down.")
  rs.setOutput("right", false)
  rs.setOutput("left", false)
  rs.setOutput("top", false)
  rs.setOutput("bottom", false)
  rs.setOutput("back", false)
  sleep(2)
  os.shutdown()
  end
else
term.clear()
term.setCursorPos(1,1)
print("Command incorrect, shutting down.")
rs.setOutput("right", false)
rs.setOutput("left", false)
rs.setOutput("top", false)
rs.setOutput("bottom", false)
rs.setOutput("back", false)
sleep (2)
os.shutdown()
end

Enjoy! :D

#12

Monkeydash

Posted 26 September 2012 - 02:59 PM

View Postlillenthaman, on 13 July 2012 - 09:44 AM, said:


and finaly the third one:
right = "Find Root Password"
debug = "reboot"
outputface = "right"
function Lock()
password()
if user == debug then
print "Rebooting Computer..."
sleep(2)
shell.run"clear"
os.reboot()
end
if user == right then
correct2()
end
if not(user == right1) or (user == debug) then
print "That is not a internal command, external command, program or commandfile"
sleep(1)
write "C:>"
user= read()
end
end
function correct2()
sleep(2)
print "Root Password <heaps of fail>"
sleep(1)
print "Computer Rebooting in 6 seconds..."
sleep(6)
shell.run"clear"
os.reboot()
end
function password()
write "C:>"
user= read()
end
Lock()

Sorry for the long post, dont know how to make spoilers :S

I did a try to edit your Third code, though i didn't find a use for the "Lock-function". here is the result:
---Variables-----
right = "Find Root Password"
debug = "reboot"
outputface = "right"
---Functions----
function Lock()
password()
if pw == debug then
print("Rebooting Computer...")
sleep(2)
shell.run("clear")
os.reboot()
end
end
function correct2()
sleep(2)
print "Root Password <heaps of fail>"
sleep(1)
print "Computer Rebooting in 6 seconds..."
sleep(6)
shell.run("clear")
os.reboot()
end
function password()
write("C:>")
user = read()
pw = user
return pw
end
----Code Part -------
password()
if pw == right then
correct2()
elseif (pw == outputface) or (pw == debug) then
print("That is not a internal command, external command, program or commandfile")
sleep(1)
write("C:>")
end


#13

CTheMadman

Posted 07 November 2012 - 09:15 PM

I'm kinda new to Lua, and I keep getting the error message that I need "=" on the line that contains end to a for loop. I know it's a noob problem, but I'd be glad to know what's wrong. It doesn't help adding an = on that line

#14

AadamZ5
    AadamZ5

    Tree Puncher

  • Members
  • 11 posts
  • Minecraft: AadamZ5

Posted 08 May 2013 - 02:40 AM

Well, I got my self a little problem, When a user types in "find" it goes to the find sector. Right? THe user enters a file that isn't there. It should detect it and say it wasn't there right? what am I doing wrong? Lines 40, 45 are giving me the most trouble. When the file isn there, i get the error "attempt to index ? (a nil value)" when it runs line 44, "h.readLine()". What am I doing wrong.





function clear()
term.clear()
term.setCursorPos(1, 1)
end

while true do
clear()
print("What would you like to do?\nValid Commands are: \nadd \nfind \ndelete\n")
local input = read()
if input == "add" then
clear()
term.write("What is the name of the item?\n")
local itemn = read()
local exist = fs.exists(itemn)
if exist == "true" then
clear()
print("Error. That already exists.")
sleep(2)
else
clear()
print("How much does it cost?")
local itemc = read()
fs.makeDir("ItemBase")
h = fs.open(itemn, "w")
h.write("item: "..itemn.." Cost: "..itemc)
h.close()
clear()
print("Item Added.")
sleep(2)
clear()
end
elseif input == "find" then
clear()
print("What would you like to find?")
local locate = read()
exist = fs.exists(locate)
if exist == "false" then
print("Error. Item not found.")
sleep(2)
else
clear()
h = fs.open(locate, "r")
local data = h.readLine()
h.close()
print(data)
sleep(5)
end
elseif input == "delete" then
print("What do you want to delete?")
local del = read()
delexist = fs.exists("ItemBase/"..del)
if delexist == "false" then
clear()
print("Error. Item not found.")
sleep(2)
else
fs.delete(del)
clear()
print("Item deleted.")
sleep(2)
end
end



end



EDIT:
Have relized that you can't put a varialble inside of fs.exists(). how do workaround?

#15

ninjaz5736
  • Minecraft: ninjaz5736

Posted 09 May 2013 - 06:39 PM

if disk.getLabel("right")=="Pass" then
  redstone.setoutput("left", true)
  sleep(5)
  redstone.setoutput("left", false)
end

It says "startup:2: attempt to call nil"

WHY?

BTW i am quite new to coding.

#16

RustyXXL
    RustyXXL

    Obsidian Miner

  • Members
  • 1262 posts
  • Location: Germany
  • Minecraft: RustyXXL

Posted 09 May 2013 - 06:44 PM

Because case sensitivity

#17

ninjaz5736
  • Minecraft: ninjaz5736

Posted 12 May 2013 - 06:58 AM

View PostRustyXXL, on 09 May 2013 - 06:44 PM, said:

Because case sensitivity
How do i fix it? Can i have some working code please?

EDIT: Sorted!

#18

ninjaz5736
  • Minecraft: ninjaz5736

Posted 12 May 2013 - 07:04 AM

View PostRustyXXL, on 09 May 2013 - 06:44 PM, said:

Because case sensitivity
Thx. I've sorted it out.

#19

MessOfGaming101

Posted 17 May 2013 - 04:29 PM

View Postninjaz5736, on 09 May 2013 - 06:39 PM, said:

if disk.getLabel("right")=="Pass" then
  redstone.setoutput("left", true)
  sleep(5)
  redstone.setoutput("left", false)
end

It says "startup:2: attempt to call nil"

WHY?

BTW i am quite new to coding.
write line 2 like this "redstone.setOutput("left", true)"
and write line four like this "redstone.setOutput("left", false)"

any other problems?

View PostSorfak, on 25 February 2012 - 08:24 PM, said:

the third time you wrote term.SetCursorPos you forgot the "m"
EDIT: i might be wrong, but don't you have to type () ofter term.clear?
you are correct about the whole term.clear situation, it is written like this "term.clear()"