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

[SOLVED] Attempt to Concatenate string and nil, Dont understand.

Started by Hayden_Almeida, 11 February 2015 - 06:57 PM
Hayden_Almeida #1
Posted 11 February 2015 - 07:57 PM
Why this is wrong?




dificuldade = read()
print("Ok.")
sleep(2)
print("Sejam bem vindos "..player1.." e "..player2..".")
if dificuldade == "1" then
local a = "Facil"
print("debug: 1")
elseif dificuldade == "2" then
local a = "Medio"
print("debug: 2")
elseif dificuldade == "3" then
local a = "Dificil"
print("debug: 3")
end
print("Voces vao jogar na dificuldade "..a)  ERROR IN THIS LINE: attempt to concatenate string and nil
Edited on 11 February 2015 - 07:53 PM
Dog #2
Posted 11 February 2015 - 08:12 PM
The error you're receiving is because you are defining 'a' as local in scope to the if statement - this makes 'a' unavailable outside the if statement. Localize 'a' prior to the if statement to eliminate that problem. I would also recommend localizing dificuldade as well…


local dificuldade = read()
print("Ok.")
sleep(2)
print("Sejam bem vindos "..player1.." e "..player2..".")
local a
if dificuldade == "1" then
  a = "Facil"
  print("debug: 1")
elseif dificuldade == "2" then
  a = "Medio"
  print("debug: 2")
elseif dificuldade == "3" then
  a = "Dificil"
  print("debug: 3")
end
print("Voces vao jogar na dificuldade "..a)
Edited on 11 February 2015 - 07:14 PM
Hayden_Almeida #3
Posted 11 February 2015 - 08:53 PM
Thanks