General > Developers Corner

File.Read/Write help.


Hi All,
I'm going to try to describe this as best I can, but I'm aware it's both a complex and unusual problem. Here we go...

Some background, to help explain how I got here... I have a script ( which stores a Table as JSON, containing {"SteamID":"DiscordID"}. This table drives a bot which Mutes players in Discord when they die in TTT/TTT2/Murder/etc. I've finished the script and I'm relatively happy with it, even if it needs some major cleanup. This is my first GMod addon, and my first time playing with (G)Lua.

Recently I've been trying to add ULX menu support. I successfully managed to add the commands (see:, which gave me something of a boost in confidence, as I've been mostly reading other developers code, and trying to understand it. My next step was to try to introduce a settings menu (similar to how the TTT and Randomat ULX menus work). After some trial and error, I actually managed it, and added a new menu under Settings (see: Again, I was elated, as this was hugely laborious work, reading through other examples, and even the source code for ULX itself.

The issue came when I tried to create a user friendly method for Admin's to add Discord ID's against Steam ID's, in the ULX menu system. What I want to do, is add a list of users who are connected, with the Discord ID listed next to there names (see:, sorry i scrubbed my ID for reasons). This works so far, when I hard code the ID JSON (EG: {"SteamID":"DiscordID"}). However, inside (, when I try to collect the cache of ID's from the file system, I get back Nil. Even when using the exact same util function (see: from the Shared.lua script. However, if I write to the file first, and then read from it, I can see the JSON, but it doesn't match the JSON in the file on the server.

What seems to be happening is File.Read/Write and ULib.fileRead/fileWrite, when in the XGUI folder, seem to be writing to some kind of Memcache layer. I don't know if this is true or not, as my experience is very small, but the evidence is pointing to that.

Side Note on why I want admins to add the connections manually from the menu: The issue comes from my own experience running a server for after work games, and a lot of these extremely intelligent programmers, don't understand how to type "!discord MyDiscordName" into chat without a lot of hand holding. Night now, I have to manually edit the connections_cache.json file and add the SteamID: DiscordID connections for new team members playing. I wanted to get away from that if I could.

If ANYONE can give me a hand figuring this out, I would be extremely grateful.
-Manix84  :)

TL;DR: I want to read and write a json file on my server. I can't seem to read/write the same file from inside lua/autorun/shared.lua and lua/ulx/xgui/settings/discord.lua. I'm really confused. For a better explanation, read above.

So, very kindly, Timmy gave me some help on the Discord server.

The core of my problem is that i'm using a read-write command on the Server in `lua/autorun/shared.lua`, and then attempting to do the same read-write on the client side in `lua/ulx/xgui/settings/discord.lua`. Meaning the latter was read-writing on my own machine, rather than the remote server (I suspect).

The solution is to use to pass the table from the server side to the client side for modification, which I will be doing later tonight :).

Start of conversation on Discord:

Exactly! The file library reads/writes files depending on the Lua state. For example: calling file.Read on the server will read a file on the server, while calling file.Read on the client will read a file on the client.

Props to you for providing detailed info and all the relevant code, which made it trivial to troubleshoot!


[0] Message Index

Go to full version