• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Loading a table from a .txt file (lua) (textToTable)

dudeim

Member
Joined
Oct 5, 2007
Messages
121
Reaction score
9
Hey all,

I've been out of ot a while so scripting is harder again:p

I'm currently trying to save a table to a txt file on my pc and then load it back to a table again, the saving is working (thanks to mock for a function) though I have no idea how I would go about loading it back to a table so I can actually use the table for scripting.
Here is the code to save the table:
Lua:
function tableToString(tb) -- by Mock
	if type(tb) ~= "table" then
		return nil, error("bad argument #1 to 'saveTable' (table expected, got "..type(tb)..")", 2)
	end
	local str = "{"
	for k,d in pairs(tb) do
		if type(k) == 'string' then
			if type(d) == 'string' then
				str = str..""..k.."='"..d.."',"
			elseif type(d) == 'number' or type(d) == 'boolean' then
				str = str..""..k.."="..tostring(d)..","
			elseif type(d) == 'table' then
				str = str..'{'
				for e,f in pairs(d) do
					if type(e) == 'string' then
						if type(f) == 'string' then
							str = str..""..e.."='"..f.."',"
						elseif type(f) == 'number' or type(e) == 'boolean' then
							str = str..""..e.."="..tostring(f)..","
						elseif type(f) == 'table' then
							str = str..""..e.."="..tableToString(f)..","
						end
					elseif type(e) == 'number' then
						if type(f) == 'string' then
							str = str.."["..e.."]='"..f.."',"
						elseif type(f) == 'number' or type(f) == 'boolean' then
							str = str.."["..e.."]="..tostring(f)..","
						elseif type(f) == 'table' then
							str = str.."["..e.."]="..tableToString(f)..","
						end
					end
				end
				str = str..'},'
			end
		elseif type(k) == 'number' then
			if type(d) == 'string' then
				str = str.."["..k.."]='"..d.."',"
			elseif type(d) == 'number' or type(d) == 'boolean' then
				str = str.."["..k.."]="..tostring(d)..","
			elseif type(d) == 'table' then
				str = str..'{'
				for e,f in pairs(d) do
					if type(e) == 'string' then
						if type(f) == 'string' then
							str = str..""..e.."='"..f.."',"
						elseif type(f) == 'number' or type(e) == 'boolean' then
							str = str..""..e.."="..tostring(f)..","
						elseif type(f) == 'table' then
							str = str..""..e.."="..tableToString(f)..","
						end
					elseif type(e) == 'number' then
						if type(f) == 'string' then
							str = str.."["..e.."]='"..f.."',"
						elseif type(f) == 'number' or type(f) == 'boolean' then
							str = str.."["..e.."]="..tostring(f)..","
						elseif type(f) == 'table' then
							str = str.."["..e.."]="..tableToString(f)..","
						end
					end
				end
				str = str..'},'
			end
		end
	end
	local str = str.."}"
	if string.sub(str,string.len(str)-2,string.len(str)-2) == "," then
		str = string.sub(str,0,string.len(str)-3).."}"
	end
	return str
end
So now I want to be able to do somthing like this:

Lua:
function bla
local table1 = { value1 = 15, value2 = 20, value3 = 117, value4 = 3}
doWriteFile(path1,tableToString(table1)
local table2 = loadTable(path1) --this can also be something like a function textToTable as that's basicly what I need
--now I should be able to do stuff with the values from table1 using table 2
local totalvalue = table2.value1+table2.value2+table2.value3+table2.value4 --this would be 15+20+117+3
end

So to sum it up, can anyone help me creating a textToTable function?
I can probably script it myself if I am pointed in the correct direction with a little example or something.

Thanks alot!
 
Back
Top