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

failed to concatenate string and nil

Started by TR1T0N_, 17 May 2014 - 03:30 PM
TR1T0N_ #1
Posted 17 May 2014 - 05:30 PM
Hello im current experiencing an error in a program that i designed a while ago however never finished the program errors on line 46 giving the error lua:46: attempt to concatenate string and nil and line 46 is rednet.send(1, "auth") so i dont understand how this is erroring any help would be much appreciated
p.s there proberly is alot more errors in this code as im currently debugging and finalizing it for release
Spoiler

function load()
print("load")
rednet.open("back")
print("activating server")
get()
end
function get()
--[[
var1 = 0
var2 = 0
var3 = 0
var4 = 0
var5 = 0
var6 = 0
var7 = 0
var8 = 0
var9 = 0
var10 = 0
var11 = 0
var12 = 0
var13 = 0
var14 = 0
var15 = 0
]]
print("get")
var1, var2, var3 = rednet.receive()
print(tostring(var1)..tostring(var2)..tostring(var3))
if var2 == "request" then request() else deny()
end
end
function request()
print("request")
var4, var5, var6 = rednet.receive()
print(tostring(var4)..tostring(var5))
if fs.exists("usernames/"..tostring(var5)) == true then
pin()
else
deny()
end
end
function pin()
print("pin")
rednet.send(1, "auth")
print("s")
var7, var8 = rednet.receive()
print(var7..var8)
h = fs.open("userpins/"..tostring(var5).."/pin", "r")
pinno = h.readAll()
h.close()
if var8 == pinno then auth() else deny()
end
end
function auth()
print("auth")
rednet.send(var7,"auth")
command()
end
function command()
state = true
print("command")
while state == true do
var10, var11, var12 = rednet.receive()
print(tostring(var10)..tostring(var11)..tostring(var12))
if var11 == "check balance" then checkbalance()
elseif var11 == "withdraw" then withdraw()
  elseif var11 == "transfer" then transfer()
   elseif var11 == "logout" then logout()
  end
end
end
function checkbalance()
print("checkbalance")
h = fs.open("userbalance/"..tostring(var5).."/balance", "r")
bal = h.readAll()
rednet.send(var10, bal)
command()
end
function withdraw()
print("withdraw")
h = fs.open("userbalance/"..tostring(var5).."/balance", "r")
bal = h.readAll()
h.close()
var13, var14, var15 = rednet.receive()
print(tostring(var13)..tostring(var14)..tostring(var15))
curbal = bal - var14
h = fs.open("userbalance/"..tostring(var5).."/balance", "w")
h.write(tostring(curbal))
h.close()
rednet.send(tostring(var13), tostring(var14).." has been deducted you current balance is "..tostring(curbal))
end
function transfer()
print("transfer")
end
function logout()
print("logout")
state = false
get()
end
function deny()
print("deny")
rednet.send(var1, "denied")
end
load()
Edited on 17 May 2014 - 03:31 PM
flaghacker #2
Posted 17 May 2014 - 10:06 PM
You have to define your functions before your call them:

a("test")
function a(text)
   print(text)
end
--error
function a(text)
   print(text)
end
a("test")
--no error
TR1T0N_ #3
Posted 18 May 2014 - 10:01 AM
but rednet.send() is already a function in the rednet api its already defined
Goof #4
Posted 18 May 2014 - 10:22 AM
…Well Its not the rednet.send() which is erroring… Its the function you're calling right after ( "get()")

And the function "get" isnt defined before its call…
I would say:
Move the "load" function to bottom of your script, then try again.

(like this:)
Spoiler

function get()
  --[[
  var1 = 0
  var2 = 0
  var3 = 0
  var4 = 0
  var5 = 0
  var6 = 0
  var7 = 0
  var8 = 0
  var9 = 0
  var10 = 0
  var11 = 0
  var12 = 0
  var13 = 0
  var14 = 0
  var15 = 0
  ]]
  print("get")
  var1, var2, var3 = rednet.receive()
  print(tostring(var1)..tostring(var2)..tostring(var3))
  if var2 == "request" then request()
  else deny()
  end
end
function request()
  print("request")
  var4, var5, var6 = rednet.receive()
  print(tostring(var4)..tostring(var5))
  if fs.exists("usernames/"..tostring(var5)) == true then
	pin()
  else
	deny()
  end
end
function pin()
  print("pin")
  rednet.send(1, "auth")
  print("s")
  var7, var8 = rednet.receive()
  print(var7..var8)
  h = fs.open("userpins/"..tostring(var5).."/pin", "r")
  pinno = h.readAll()
  h.close()
  if var8 == pinno then auth()
  else deny()
  end
end
function auth()
  print("auth")
  rednet.send(var7,"auth")
  command()
end
function command()
  state = true
  print("command")
  while state == true do
	var10, var11, var12 = rednet.receive()
	print(tostring(var10)..tostring(var11)..tostring(var12))
	if var11 == "check balance" then checkbalance()
	elseif var11 == "withdraw" then withdraw()
	elseif var11 == "transfer" then transfer()
	elseif var11 == "logout" then logout()
	end
  end
end
function checkbalance()
  print("checkbalance")
  h = fs.open("userbalance/"..tostring(var5).."/balance", "r")
  bal = h.readAll()
  rednet.send(var10, bal)
  command()
end
function withdraw()
  print("withdraw")
  h = fs.open("userbalance/"..tostring(var5).."/balance", "r")
  bal = h.readAll()
  h.close()
  var13, var14, var15 = rednet.receive()
  print(tostring(var13)..tostring(var14)..tostring(var15))
  curbal = bal - var14
  h = fs.open("userbalance/"..tostring(var5).."/balance", "w")
  h.write(tostring(curbal))
  h.close()
  rednet.send(tostring(var13), tostring(var14).." has been deducted you current balance is "..tostring(curbal))
end
function transfer()
  print("transfer")
end
function logout()
  print("logout")
  state = false
  get()
end
function deny()
  print("deny")
  rednet.send(var1, "denied")
end
function load() -- put it here instead, so all functions its going to call, is defined.
  print("load")
  rednet.open("back")
  print("activating server")
  get()
end
load()

Hope it helps
CometWolf #5
Posted 18 May 2014 - 10:39 AM
The order of definition dosen't matter in this case. Because all his variables are global, and all the functions are defined prior to actually being called, since they are all called through the load function at the end.
line 46 is actually

print(var7..var8)
which is the result of line 45

var7, var8 = rednet.receive()
Given the error, we know that the second variable, var8, is nil.
What are you sending to the computer?
TR1T0N_ #6
Posted 18 May 2014 - 10:47 AM
thanks alot for your help. kinda stupid mistake on my side but thanks for you help