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

[ComputerCraft] startup:16: attempt to call string

Started by GhostFrag1, 30 July 2014 - 01:47 PM
GhostFrag1 #1
Posted 30 July 2014 - 03:47 PM
I have been writing a lock for my computers from scratch in lua for computercraft but have encountered an error. The error is the startup:16: attempt to call string as the title says here is the code I have come up with for the lock.

username = "qwerty"
username2 = "qwerty"

password = "123"
password2 = "123"

openTime = 5
rebootTime = 2
waitTime = 3

while true do
os.pullEvent =os.pullEventRaw
term.clear()
term.setCursorPos(1, 1)
textutils.slowPrint("Frag Lock V1.2")
username()

function username()
write("Username: ")
usernameinput = read()
if usernameinput == username or username2 then
  password()
else
  textutils.slowPrint("Username Incorrect please try again!")
  usernameincorrect()
end
end

function password()
write("Password: ")
passwordinput = read()
if passwordinput == password or password2 then
  correct()
else
  passwordincorrect()
end
end

function correct()
redstone.setOutput("left", true)
sleep(openTime)
redstone.setOutput("left", false)
textutils.slowPrint("Rebooting...")
sleep(rebootTime)
os.reboot()
end

function usernameincorrect()
textutils.slowPrint("Username Incorrect! Try again later.")
sleep(waitTime)
textutils.slowPrint("Rebooting...")
sleep(rebootTime)
os.reboot()
end

function passwordincorrect()
textutils.slowPrint("Password Incorrect! Try again later.")
sleep(waitTime)
textutils.slowPrint("Rebooting...")
sleep(rebootTime)
os.reboot()
end
end

Any help will be appreciated, thanks.
Lyqyd #2
Posted 30 July 2014 - 03:54 PM
You define the variable username as a string at the top of your script, then you attempt to call username (and it's a string, hence the error), and then you redefine username as a function. You should move your function definitions to the top of the script and make them local, and make sure you aren't trying to use one variable (like username) to hold two things at the same time (like a function and a string).
GhostFrag1 #3
Posted 30 July 2014 - 03:59 PM
You define the variable username as a string at the top of your script, then you attempt to call username (and it's a string, hence the error), and then you redefine username as a function. You should move your function definitions to the top of the script and make them local, and make sure you aren't trying to use one variable (like username) to hold two things at the same time (like a function and a string).

My bad, was a silly mistake (I'm just starting lua) also I renamed the functions and gave them a capital letter at the start so not it is for example it is now function Username. That got rid of the first error not it is giving the error of startup:16: attempt to call nil. I also made sure that where the function is being called I gave that a capital too, but the new error still remains. Thanks.
Lyqyd #4
Posted 30 July 2014 - 04:52 PM
Did you also move the function declarations to the top of the file?
GhostFrag1 #5
Posted 30 July 2014 - 06:10 PM
Did you also move the function declarations to the top of the file?

Misread the previous reply, done that and it is fixed, thanks again.