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

[ERROR] textutils:304: attempt to concatenate string and nil

Started by stew3254, 09 August 2016 - 06:54 PM
stew3254 #1
Posted 09 August 2016 - 08:54 PM
I have been having an issue with what I am pretty sure is when I unserialize a table from another file. I am positive the table is serialized correctly and read correctly, but whenever I try to unserialize anything from a file all I get is the 'attempt to concatenate string and nil'. I can get unserialize to work when not reading another file, but that isn't doing me any luck here. Can someone please help me? Thanks ahead. This is the code I get the error from.
Spoiler

local tArgs = {...}
if tArgs[1] == "sethome" then
  local hx,hy,hz = gps.locate()
  local coords = {x = hx, y = hy, z = hz}
  if fs.exists("home") then
	fs.delete("home")
  end
  local file = fs.open("home","w")
  file.write(textutils.serialize(coords))
  file.close()
  print("Home set at "..hx..", "..hy..", "..hz)
end
if tArgs[1] == "home" then
  if fs.exists("home") then
	local file = fs.open("home","r")
	local data = file.readAll()
	file.close()
	local hx = textutils.unserialize(data.x)
	local hy = textutils.unserialize(data.y)
	local hz = textutils.unserialize(data.z)
	print("Home is at "..hx..", "..hy..", "..hz)
  else
	print("Home is not set yet.")
	print("To set a home type 'goto sethome'")
  end
end
Here is the table
Spoiler

{
  z = -798,
  y = 237,
  x = -397,
}
Dragon53535 #2
Posted 09 August 2016 - 11:46 PM
Can you give the full error? It should reference the file the error it occured in as well as the line.

Edit: Nevermind, you're unserializing incorrectly.

Data returned from file.readAll is just a string, you need to unserialize that and then reference the x y and z from that.


local data = textutils.unserialize(file.readAll())
data.x
data.y
data.z
Edited on 09 August 2016 - 09:48 PM
stew3254 #3
Posted 10 August 2016 - 01:14 AM
Data returned from file.readAll is just a string, you need to unserialize that and then reference the x y and z from that.

local data = textutils.unserialize(file.readAll())
data.x
data
.y
data.z
Thanks so much.