• file.Write not creating files
    18 replies, posted
so i am trying to write stuff to data and file.Write isn't making data files. I don't know what tom put or how to fix it. Here is an instance I am talking about - [lua] local function checkHasFile( ply ) if file.Exists( "darkrppermastore/"..ply:SteamID()..".txt", "DATA" ) then return true else if file.Exists( "darkrppermastore", "DATA" ) then local plyFile = {} file.Write( "darkrppermastore/"..ply:SteamID()..".txt", util.TableToJSON( plyFile ) ) else file.CreateDir( "darkrppermastore" ) end end end [/lua]
file.CreateDir will return if the folder already exists, so running the function everytime you start your server wont matter. [lua] file.CreateDir( "darkrppermastore" ) local function checkHasFile( ply ) if file.Exists( "darkrppermastore/"..ply:SteamID()..".txt", "DATA" ) then return true else file.Write( "darkrppermastore/"..ply:SteamID()..".txt", util.TableToJSON( plyFile ) ) end end [/lua]
this isn't creating a data file
You can't use ply:SteamID() in filenames due to the ":". try ply:SteamID():gsub(":","_")
of that allows me to write files, but opening them doesn't seem to be working.
How are you reading/opening it?
reading, should i be opening?
You didn't answer my question.
file.Read( "darkrppermastore/"..ply:SteamID():gsub(":","_")..".txt", "DATA" )
I am using [lua]file.Write( "darkrppermastore/"..ply:SteamID():gsub(":","_")..".txt", util.TableToJSON( {} ) )[/lua] to write the files (seems to work), and using [lua]file.Open( "darkrppermastore/"..ply:SteamID():gsub(":","_")..".txt", "r", "DATA" )[/lua] to open it again, but now the error says that it is getting user data and not tables, why?
You know, the full error might have helped a bit more.
[QUOTE=jack10685;41759069]I am using [lua]file.Write( "darkrppermastore/"..ply:SteamID():gsub(":","_")..".txt", util.TableToJSON( {} ) )[/lua] to write the files (seems to work), and using [lua]file.Open( "darkrppermastore/"..ply:SteamID():gsub(":","_")..".txt", "r", "DATA" )[/lua] to open it again, but now the error says that it is getting user data and not tables, why?[/QUOTE] Are you trying to use file.Open as a text output? If so you don't do that. [lua] local my_file = file.Open("blahblahblah", "r", "DATA") local my_files_text = my_file:Read(my_file:Size()) [/lua]
Just use file.Read.
[QUOTE=jack10685;41759069]I am using [lua]file.Write( "darkrppermastore/"..ply:SteamID():gsub(":","_")..".txt", util.TableToJSON( {} ) )[/lua] to write the files (seems to work), and using [lua]file.Open( "darkrppermastore/"..ply:SteamID():gsub(":","_")..".txt", "r", "DATA" )[/lua] to open it again, but now the error says that it is getting user data and not tables, why?[/QUOTE] file.Read works just fine. [code] ] lua_run pmeta = FindMetaTable( "Player" ) > pmeta = FindMetaTable( "Player" )... ] lua_run function pmeta:TrimID() return self:SteamID():gsub( ":", "_" ) end > function pmeta:TrimID() return self:SteamID():gsub( ":", "_" ) end... ] lua_run file.Write( Player( 2 ):TrimID()..".txt", util.TableToJSON( { test = "hi" } ) ) > file.Write( Player( 2 ):TrimID()..".txt", util.TableToJSON( { test = "hi" } ) )... ] lua_run print( file.Read( Player( 2 ):TrimID()..".txt", "DATA" ) ) > print( file.Read( Player( 2 ):TrimID()..".txt", "DATA" ) )... {"test":"hi"} ] lua_run PrintTable( util.JSONToTable( file.Read( Player( 2 ):TrimID()..".txt", "DATA" ) ) ) > PrintTable( util.JSONToTable( file.Read( Player( 2 ):TrimID()..".txt", "DATA" ) ) )... test = hi [/code] Or just do what Meep said: [lua] f = file.Open( "path/to/file.txt", "r", "DATA" ) print( f:Read( f:Size() ) ) [/lua]
when I use file.Read = [lua] [ERROR] addons/darkrp permanent store/lua/drp-ps/cl_init.lua:22: bad argument #1 to 'JSONToTable' (string expected, got no value) 1. JSONToTable - [C]:-1 2. Function - addons/darkrp permanent store/lua/drp-ps/cl_init.lua:22 3. unknown - lua/includes/modules/usermessage.lua:87 [/lua] the data file does exist and it has values in it, yet I get this
Post some code along with your error perhaps?
writing the file - [lua]if file.Exists( "darkrppermastore/"..LocalPlayer():SteamID():gsub(":","_")..".txt", "DATA" ) then plyPermaFile = util.JSONToTable( file.Read( "darkrppermastore/"..LocalPlayer():SteamID():gsub(":","_")..".txt", "DATA" ) ) else file.Write( "darkrppermastore/"..LocalPlayer():SteamID():gsub(":","_")..".txt", util.TableToJSON( { placeholder = true } ) ) plyPermaFile = {} end[/lua] reading it - [lua]local plyPermaFile = util.JSONToTable( file.Read( "darkrppermastore/"..ply:SteamID():gsub(":","_")..".txt", "DATA" ) )[/lua] there is reading in both snippets, didn't realize when i copied it
It seems to me as if you're creating the file clientside and trying to read from it serverside.
[QUOTE=EvacX;41761725]It seems to me as if you're creating the file clientside and trying to read from it serverside.[/QUOTE] so should i create and read from client or server side?
Sorry, you need to Log In to post a reply to this thread.