How to write into a custom folder inside the data folder ?
12 replies, posted
Hi,
I'm trying to keep track of new player in my server, i'm using this code atm :
file.Append("newusers.txt", steamID .. "\n")
It works fine but i wanted to have a folder in the data folder containing this newusers.txt.
But it's just not working I tried :
file.Append("test/newusers.txt", steamID .. "\n")
But the test folder stay empty.
Any idea ?
Thanks in advance
Does the file "test.newusers.txt" actually exist?
The file "test.newusers.txt" doesn't exist no, but there is a file called 'newusers.txt' into a folder 'test' and the folder test is into the data folder
My bad that's what I meant to ask. Typo. You say the test folder stays empty? Do you mean the folder, or the contents of it's files?
Use file.IsDir, and file.CreateDir
Typijg from my phone right now so formatting might be off but this should work:
— Somewhere in code
if not file.IsDir(“MyFolder”) then
file.CreateDir(“MyFolder”)
end
— Later, elsewhere in code
file.Append(“MyFolder/MyFile.txt”, myData)
The folder is allready created, i even created .txt in it. Still not working
Can we see the code please?
local function InsertNewPlayer( ply )
local steamID = ply:SteamID()
sql.Query( "INSERT INTO T_PLAYER_INFOVG_PLY (`PLY_STEAMID`, `PLY_VG`, `PLY_VG_TR`,`PLY_USERGROUP` )VALUES ('".. steamID .."' , '0', '0', 'user')")
local result = sql.Query("SELECT PLY_STEAMID FROM T_PLAYER_INFOVG_PLY WHERE PLY_STEAMID = '"..steamID.."'")
if result then
Msg("Player has been created and added to T_PLAYER_INFOVG_PLY")
file.Append("users_list.txt", ply:Name() .." [" ..steamID .. "]\n")
else
--Msg("Something went wrong with InsertNewPlayer query FUCK")
end
end
local function CheckIfPlayerExists( ply )
local steamID = ply:SteamID()
local result = sql.Query("SELECT PLY_STEAMID FROM T_PLAYER_INFOVG_PLY WHERE PLY_STEAMID = '"..steamID.."'")
if result then
local isVG = sql.QueryValue("SELECT PLY_VG FROM T_PLAYER_INFOVG_PLY WHERE PLY_STEAMID = '"..steamID.."'")
if isVG == "1" then
Msg("Player exists and is Vergeron")
local timeRemaining = sql.QueryValue("SELECT PLY_VG_TR FROM T_PLAYER_INFOVG_PLY WHERE PLY_STEAMID = '"..steamID.."'")
MakeVergeron( ply , timeRemaining )
else
--ply:SetVar("vgTimeRemaining", 0)
end
else
InsertNewPlayer( ply )
end
end
local function HandlePlayerInitialSpawn( ply )
timer.Create("steam_id_delay", 1, 1, function()
CheckIfPlayerExists( ply )
end)
end
hook.Add( "PlayerInitialSpawn", "PlayerInitialSpawn", HandlePlayerInitialSpawn )
if not file.IsDir("test", "DATA") then
file.CreateDir("test")
end
hook.Add("PlayerInitialSpawn", "Test", function(ply)
file.Append("test/test.txt", ply:Nick().." ["..ply:SteamID().."]\n")
end)
This works completely fine. I'm almost 100% sure it has to do with the your SQL queries. Is "Player has been created and added to T_PLAYER_INFOVG_PLY" being printed?
Yes. It does
Like i said if i remove the test/ it works fine
It could be your file permissions. What operating system are you testing on? If it’s Linux make sure the owner and group are set accordingly. Chmod go 755 I believe, that should allow reading and writing.
If you use file.Write("path/file.txt", string) it will make the directory (path/) if it does not exist. It will create the file if it does not exist and if it already does, it will overwrite it. It will have the contents of string.
I'm not entirely sure what you are using file.Append() for but there might be something I'm missing idk probabaly
file.Write overwrites everything in the file whereas file.Append adds to the file.
Sorry, you need to Log In to post a reply to this thread.