Storing small data

Hello there.

How does one store data in lua?
What I want to do is store an integer, so player can save/load/quit/rejoin the game and it will be saved and loaded.

What’s the best way to achieve this?

It depends on what you need to do. If you need to store data for like user preferences and settings then you would store it on the client using things like file.Write which would write to the data folder of the clients garrysmod folder.

However, if you need to store things like level and xp then you would want to store those on a serverside database.

There are numerous ways to do this and really it depends on whats best for you.

Some of the ways:

-File writing: Not ideal, but its quick and easy to export, just not very good for cross-server use.
-SQLlite: Great for most of the server hosts that you use now a days instead of renting out a serverbox. This is also built into the gamemode
-Mysql: With the help of a few binary libraries that you can pickup on azuisleet’s google code page (]). The downside? If you are trying to contact a remote mysql database on your server, things can get pretty laggy and it is most definitely not that good of an idea unless you really know how to optimize all your saves and writes to it.

You could simply use playerPerson:SetPData(key, value)

If you really wanna store small serverside data amounts, I’d use file.Write, and use a loading system with player files named via UniqueID.

Longer data streams, like full amounts of information (key SteamID, you wanna store their weaponry, health, armor, XP, etc) would require MySQL or SQLite to be performed competently.

Thanks for the help guys!

Meh, but then the problem of clutter arises. Honestly, if you’re going to be storing data at all on serverside, just use a database (unless something like PData fits your needs better). It’s organized and highly expandable. It’s also faster and more memory-efficient than accessing text files, which is the point of using them in the first place.

Misclicked. Agreed.

Well at the moment all I need is a simple way of storing an integer value :stuck_out_tongue:

Oh, well yeah. The clutter issue would arise if you suddenly needed to store some integers for different things, and being used to using files you continued to use them, causing clutter.

But yeah, for a single integer, just use a text file.

Thanks a lot!