Files aren't saving!

So I’m trying to save data into a file, and it works perfectly fine when on my own dedicated server, however if I upload the addon to a paid server’s File Manager, it won’t save any data, even though it works perfectly fine. There’s no error accompanying the issue according to the console. I’m really confused.

Thanks in advance :slight_smile:

Here’s the code so you can scan it for flaws if you want:


local tick = 0 -- The value that controlls whether or not the Shop NPC spawns or not. 0 means it's spawnable.
local spawn = {}


spawn.highend = Vector(-1190.913330, -375.642639, 128.031250)
spawn.tatooine = Vector(-3125.892090, 6214.809570, 1088.032837)
spawn.venator = Vector(-1247.786133, -2543.968750, -4799.968750)

local function SpawnShop()
	local shop = ents.Create("swshop")
	shop:SetPos( spawn.highend )
	shop:Spawn()
end

function Initialize()
	if not file.IsDir( "AData", "DATA" ) then
		file.CreateDir( "AData", "DATA" )

		print("[UNHOLY NPC] Data Directory doesn't exist, creating directory...")
		if file.IsDir( "AData", "DATA" ) then
			print("[UNHOLY NPC] Data Directory created.")
		else
			print("[UNHOLY NPC] Data directory creation failed.")
		end
	end
end

function LoadData( ply )
	local userDataFile = "AData/" .. ply:UniqueID() .. ".txt"

	if file.Exists( userDataFile, "DATA" ) then
		print("[UNHOLY NPC] Data file EXISTS for player " .. ply:Nick() .. " (" .. ply:UniqueID() .. "), ata file loaded." )
    else

		data = {
		credits = 100,
		accessory_HP = 0,
		accessory_TEST = 0
	    }
	    file.Write( userDataFile, util.TableToJSON( data ) )

	    print("[UNHOLY NPC] Data DOESN'T EXIST for player " .. ply:Nick() .. " (" .. ply:UniqueID() .. "), writing data file." )

	    if file.Exists( userDataFile, "DATA" ) then
	    	print("[UNHOLY NPC] Data file created for player " .. ply:Nick() .. ".")
	    else
	    	print("[UNHOLY NPC] Data file creation failed! (" .. ply:UniqueID() .. ")")
	    end

	end

    timer.Create("TimedCredits", 300, 0, function()
    	if ( IsValid(ply) and ply:IsConnected() ) then
    	    local playerData = util.JSONToTable( file.Read( userDataFile, "DATA") )

            playerData.credits = playerData.credits + 10
	        ply:SendLua( [[ chat.AddText( Color(120, 120, 255), "You just got credits! Your new balance is: ", Color( 0, 0, 255), "]] .. playerData.credits .. [[") ]] )

	        file.Write( userDataFile, util.TableToJSON( playerData ) )
        end
    end)

    if ( tick == 0 ) then
    	tick = tick + 1
    	SpawnShop()

    	print("[UNHOLY NPC] Shop has spawned...")
    end
end

hook.Add("Initialize", "InitializeData", Initialize)
hook.Add("PlayerInitialSpawn", "LoadPlayerData", LoadData)

What do your prints say? Does it say “Data file creation failed!” or " Data file created"? Are other addons able to write data on your paid server? Does the AData folder exist?

In the Initialize function, it checks whether or not the directory of the data has been created. In this case, the directory is: “…/garrysmod/data/adata/”. If the directory doesn’t exist, create it, and print to the console that the directory was missing. Once it has done that, it’s going to check it’s existance again, printing whether or not the directory exists now.

In the LoadData function, it does the same thing pretty much. The only difference is, instead of it checking the existance of a directory, it’s just a file now.

Hope that answered the question about how the printing works. To answer the question whether or not other addons are able to store data on the server, yes, they are. It does create the directory, since it does exist.

Is it because I’m spelling it capital? Like: “AData” instead of “adata”? It resulted in not recognizing an entity I made stored inside the addon itself. I called it: “Shop NPC”, and fixed it by renaming it: “swshop”. I know Lua is case sensitive, and I notice it’s not creating the folder with those capital letters, instead, in lower case.

Not sure if this would solve your issue, but I just happen to notice that you create your timer inside load data. There will only every be one timer because you use the same timer name for every player and only the last one who joined will save every five minutes. You need to make one timer that loops over every player or create unique names for each timer.

You should definitely localize your “Initialize” and “LoadData” functions, and to make a unique timer, just change timer.Create(“TimedCredits”, 300, 0, function() to timer.Create(“TimedCredits”…ply:UniqueID(), 300, 0, function()

  1. UniqueID returns a number value, not a string so just incase chuck a isstring() around UniqueID().
  2. Check if the AData file is being created in your directory, if its not then create the file test again, and if that doesn’t succeed try pasting the lua files in garrysmod/lua/autorun etc.
  3. Why are you using Data Files? Just use PData, because if you are using a Dedi I recommend using MySQL, because I can see no other reason to use file.write other then to cross network the currency.

The problem I mentioned before appears to have been the issue, and i made it lowercase. It now works. But yeah, I noticed that it only gives credits for one person at a time. Thanks for the great help, I’ll use it to improve my script. Thanks.