Skip to content

Addon example: Saving Data (LevelSystem)

This page will go over how to save data between rejoins, in this example the level and experience points of a player.

This is an intermediate example. For more details about addon creation visit the first few examples.

LevelSystem logic

We need to atleast be able to save and load the level of a player, so we need a LoadLevel and SaveLevel function.
To make it easier to use we will also create a GetLevel and SetLevel function.
And, ofcourse, we also need an AddExperience function, or else players won't be able to level up.

All of these functions are serverside (saving and loading the levels is done serverside) and the GetLevel function should be shared, so that it can be used in HUDs and other clientside displays.

For storing the level of a player we will use the sqlite database inbuilt into garrysmod.
This means we will use the sql.Query function.
The database structure is very simple: We want to save EXPERIENCE and the LEVEL of a PLAYER.
This gives us 3 columns: The SteamID, level and experience.

The code

First we will create the LoadLevel function.
This function loads the level of a player or, if there is no entry for that player yet, create a new entry for the player.

The code will first select the data from the play

To see a finished HUD written like this you can visit the following link:

https://github.com/OverlordAkise/darkrp-addons/blob/main/luctus_hud/lua/autorun/client/cl_luctus_hud.lua