How to start scripting Fivem server

Non-Framework Lua Learners Guide (FiveM edition)

If you’re completely new to LUA or ANY programing languages, you’re going to want to keep a companion with you as you learn. Enter the lua programming manual:
https://www.lua.org/pil/contents.html
AND NO, YOU DON’T HAVE TO READ THE WHOLE BOOK…


Part 1: The basics

…Start with the lua programming manual chapters 2, 3, 4, 5, and 7.

These will cover 70% of the lua scripts you see in “releases”

It’ll seem overwhelming at first, but remember, this knowledge isn’t going anywhere. Come back in a few months and it’ll most likely be the same. Meaning use it as you need it, but go over the above chapters at LEAST once so that if you get lost, you won’t be COMPLETELY lost.
(You’ll have remembered seeing it somewhere, and you’ll know how to ask for help if you need to)

That will give you a base - or the fundamentals.

Once you understand the general idea of the programming language (what does and can’t do, where it should be used, and where it should NOT be used), then you can start following along with tutorials


Part 2: Try to keep up!

Now that you have a base down, you can start learning about how it applies to fiveM.
By this point you should have an idea of what functions are.
FiveM uses many different functions for many different purposes. One of the most common types of GTAV-specific functions used are called “natives.”

Natives are just a fancy word for functions. Specifically functions that make your GTA V game do cool shit. (I think that was a famous quote). There’s a lot of natives. Many you’ll use EVERY TIME YOU CREATE A SCRIPT. Many you’ll ignore and that’s okay.
A full list of natives can be found here:
MOSTLY FULL LIST OF NATIVES

With your foundational knowledge and knowledge of natives, you can start following along tutorials confidently.

If you’re someone who learns by reading, as @ChristopherM mentioned, you have:

If you are indeed a visual learner, the two best playlists by far have been:

Both are great and do a good job of providing the WHY which is very important (to me) when learning.

Get caught up on whichever resource you resonate with most and move on to the next section.
Note: you may need 2 or more resources if it doesn’t make sense. That’s okay. Be patient.


Part 3: Now it’s your turn

Now you should have an idea of Lua Scripting, and LUA scripting as it pertains to fiveM and GTA V. You should have a general idea of what the language does and can’t do, and more or less what GTA V can and can’t do as well.
You should also be familiar with events, and a few other topics.

The advanced beginners will be familiar with engine limitations, less documented features like promises, state-bags, cross-resource dependencies, or Exports, and maybe even convars. If not, that’s okay.

The goal of part 3 is to bring an idea to life. This idea can be either:

  • a part of the game that rockstar fell short on (called a “Quality of life fix”),
  • a feature that would make the game more enjoyable
  • something that you and your friends can enjoy together.

It doesn’t have to be ambitious or a huge project. Think of a simple script.

If you can’t immediately think of something, meditate on it for a few days. For me, it started with fixing aircraft handling. Another one was a script that allowed you to see the physical edge of the default map so you don’t fly out too far.

It doesn’t have to be that basic, but it also doesn’t have to be crazy huge. Start with something you KNOW you can do. Something you may get tripped up on a little, but that you know is possible. I’m a bit embarrassed on the above post, but I’ve come a long way and am proud of that I didn’t stop being ‘annoying’ about learning!

Lastly, don’t worry about making it performant, or perfect. Just make it work.


Part 4: HARDER, BETTER, FASTER, STRONGER!

Part 4 involves optimization. You should know about the basics, and how to become somewhat useful by this point. BUT YOU CAN DO MORE!

For part 4, I want you to improve your scripts in terms of quality, performance, SECURITY, and consistency.

For example, when it comes to the player experience, FRAMERATE actually matters. For example, when calculating points, if you calculate points every frame, then players with higher fps will get more points than players with lower fps

You’ll also want to work on reducing the run-time of your existing scripts. The runtime is how much of the CPU’s time your client AND SERVER scripts you’re using.

(Simply put, the less time your CPU uses ‘thinking’ about your scripts, the better. Less energy used, more time to do other ‘things’)

But wetter, how do I know what my script performance is?

That’s where you’ll want to use tools like the profiler and the resource monitor to observe how long your script is taking to run, and seeing if it can be reduced to the acceptable 0-4ms rate.

There’s also the security aspect. You should NEVER, EVER, EVER trust the client because the data that the client sends may be intentionally wrong at best, and malicious at worst.
For security, the question is:

  1. Not only: “How do I protect the server and sanitize the data coming from the client”
    but ALSO
  2. How do I log what’s not legit so that I can manually or automatically take action later?

There are also other optimizations such as manually running math functions yourself instead of relying on natives, and a few other things. This can get pretty rabbit hole-y so don’t get lost.

Once you’re comfortable, try to find a free script here in the FiveM Releases section. See if there’s anywhere you can improve. Once you’ve improved 2-3 simple scripts, you can move on


Part 5: Capstone

The final part is where you put it all together.

This is where you come up with something ambitious. This can be a small party game, racing game, or a fivem system, or a demonstration of an experiment or something similar. It must combine what you know and have the intercommunication of at least two scripts working together.

This path isn’t the best, and leaves out things such as frameworks, infrastructure setup, interiors, proper logging practices, intermediate programming techniques, advanced programming techniques, however, this guide will hopefully take you from a straight-BEGINNER to a fairly moderate INTERMEDIATE / self sufficient programmer. But don’t feel obligated to take this path. The important part is that you find one that works for you if this isn’t it. Good luck!