What do I need to know to make my own gamemode?

Hi everyone. this thread will probably be pretty massive, as I intend to gather information about all vital parts of gamemode creation. Sure, the tutorials on the gmod wiki are pretty good, but they do not really explain why it works like it does. It’s mostly “place this code here, and that code there”. I will probably ask questions that have already been answered. I have searched, but to be honest I really suck at searching since I never get the search words right. My ambition is that all info needed to create a gmod gamemode can be gathered in one place, so it will be easier for everyone to learn.

The first thing that should be asked is:
What do you consider to be needed to learn in order to develop a gamemode?

Since I’m a beginner, my guess is:
- 1. How do I know if I should place a code inside Shared.lua, cl_init.lua or init.lua?
After reading tutorials, it’s pretty obvious what their difference are (server, client, or shared), but it’s not very clear how to know when to put wihch piece of code where. Are there any general, good guidelines to follow?

- 2. How do I create stats & variables for players to use?
We maybe want to give the player a “money” varaible

- 3. How do I send data between the clients & the server?
What I know so far is that you can use datastream and/or usermessages.

- 4. How do I make the server save the player’s stats until the next time he joins?
Example: We want to remember the player’s “money” variable the next time he joins.

- 5. What exactly is a hook and how should it be used?
I tried the example code found here. I placed it in cl_init.lua, but I didn’t get it to work (only tried single player, and by dying from worldspawn/entities/npcs). it didn’t give me an error, it simply didn’t show anything in the console upon death. my guess is that it should not be placed in cl_init.lua (but how can I know where it should be placed?)
I don’t know (exactly) what the arguments mean, but i think that’s easy to learn from the wiki

Here are some of the questions I’m wondering about, even after reading on the wiki. the lua language is simple enough, but how to use it is a whole other story. Questions can be answered with links if the link provides good info and explains in which context to use it. like i said before, some of the questions are maybe answered before… my search skill failed me though… Also, if you know anything apart from those questions that you should know when creating a gamemode, please do share.

Hope I made everything clear enough…

For starters you need to know the lua syntax, thats probably the easiest thing to learn, only issue would be remembering and getting used to that.

There are three sides to GMod lua (four if you want to get technical)Server, Client, and Shared. They are pretty self explanatory, server is code ran on the server, client is code ran on the client, and shared ran on both the server and the client.

For storing information that will save after a client connects you should use Mysql, MysqLite, or use PData. PData uses MysqLite but is a lot simpler to use. MysqLite for the most part is pretty simple, but Mysql is a little more complex and you need an external database for a Mysql server but you can hook the data to a website or something, which you can’t do with MysqlLite or PData.

When you are on the GMod wiki

stands for something done on the server,

stands for something done on the client, and

stands for something done on both the client and the server.

To send data between the server and the client, you can usually use usermesages, but if you have to send alot of data or tables you can use datastream.

Cl stands for client and sh stands for shared. So cl_init is for the clients init and sv_init or init is for the server. The name dosent matter and is just used for organization, you don’t need to have it but it usually makes your file easier to find.

Also, make sure you are using NPP with the GLuaLexer plugin installed!

I’m a pretty experienced coder in general, so the lua syntax will be a piece of cake.
I already knew the difference between server, client, and shared, but you really helped me out with those color coded boxes :)! that was exactly what I was after!

so, to save the players data I gotta learn mySQL? I’ll look into it ^^.

About datastreams - I’ve heard it will no longer work with gmod 13. is that really true?

if the filenames of the lua files doesn’t matter, how does garrysmod know if it’s server/client/shared o.O?

And the first thing I did was to get NPP and install the Gmod Lua plugin :smiley:

You could save it to either sql using the module shipped with gmod or saving data to text files with glon.

Yes it’s true, however datastream shouldn’t really be used at any time. If you have the need to send a extremely huge table then you’re doing something wrong. Usermessages are the best way of transferring data from the server to the client currently. However the net library will probably be more than enough when it’s released with the newest version of gmod.
Currently console commands are the only way to send client -> server, since the net library isn’t here yet.

Because all files originate from either the serverside lua or clientside lua, in a gamemode the server starts in init.lua and the clients starts with cl_init.lua (these files are required to be in a gamemode). Shared doesn’t really ‘exist’ since it’s not synced in any way automatically between the client and the server, it’s mostly used for functions that will behave the exact same way clientside and serverside or perhaps tables that can be accessed by both the server and the client.

yes,datastream is gone in gmod 13, if you want to make a gamemode now, try to get a beta key, or just don’t use datastream etc

– Glon example incase you’re wondering.

function glon.saveTableToFile( tbl, file )
if ( tbl == nil or file == nil ) then return end;
file.Write( glon.encode(tbl), file );

function glon.loadTableFromFile(file)
if ( file == nil ) then return end;
if ( !file.Exists(file) ) then return end;

local t = glon.decode( file.Read(file) );

return t or nil;


No, you can use Mysql, MysqLite, or PData. PData is just a simple function where you can do like ply:SetPData(“money”,“500”) etc.

To get a lua file to run you need to have something in lua/autorun/server or something of the sort, from there you can use AddCSLuaFile and that sends it to the client, then from the file you used AddCSLuaFile on will run if its in lua/autorun, but to send a file to the client you use AddCSLuaFile and then include it off of the file being initially ran, sorry if I am being misleading or confusing.

“What do I need to know to make my own gamemode?

Quick question:
if I encode a table with glon and send it to the client via umsg, and then decode it on the client, will the table have EXACTLY the same structure?