Gettingstarted with scripting for FiveM might be a tad overwhelming, given the wide range of possibilities and the sparsely spread documentation. In this quick and simple guide, we'll try to show you how to get started with a quick resource in Lua.
A resource is, simply said, a collection of files that can be individually started, stopped and restarted. Your server-data folder (assuming you already installed a server) should have a resources folder already, with a few resources in them already.
If you're working on your own resources, you'll probably want to make a resources/[local] directory - this one will be ignored by Git when updating the server-data root. In there, we'll make a resources/[local]/mymode folder, since we're making, well, a gametype using the mapmanager system.
A resource folder (you know, this mymode you made above) will need a manifest to be detected by FiveM. Since this is a game type, it'll need some extra information as well to teach mapmanager about the fact that this is a game type.
Any new resource you make will probably want the latest game features. This is what the fx_version is for. You can read up on it elsewhere on this documentation site, if you ever feel the need to know more.To specify if this resource is for gta5, rdr3, or common, you should use the game variable.
The resource_type, on the other hand, tells mapmanager that this, in fact, is a game type, and that it's called "My awesome game type!". If you're just making a 'standalone' add-on resource, you probably don't want to include a resource_type line.
Finally, the client_script indicates to the scripting runtime that the client should load a script, named mymode_client.lua. If this were a JS script, it'd say mymode_client.js, or if it were C#, it'd probably be MyModeClient.net.dll, but for now we're teaching Lua so just forget that.
A quick mention of the difference between client and server scripts: most of what you'll do in FiveM will be done using client scripts, since in current versions there's no interaction with game functionality in server scripts. Server scripts should be used to have scripted actions occur across clients (using client/server events), and to provide a 'source of trust' for various actions, such as storing/loading things in a persistent database.
Since spawning a player is pretty much entirely game interaction, this happens on the client side. Every player that's joined will have a local instance of each client script running on their PC, with no shared variables or context between them.
Finally, execute start mymode in the console, and connect to your server using the FiveM client's handy localhost button in developer mode (or just enter localhost on the direct connect tab, or if you used the default port click this useful link on the PC you have FiveM installed on).
You'll probably want to do more. For this, you're going to have to learn how to call natives, which has nothing to do with indigenous people and actually are a R* label for 'game-defined script functions'. There's a lot of intricacies involved in calling natives properly - for a full reference, see the special section for this - but we'll start simple for now.
Starting already, we see a call to a function. We did not define that function. Well, we (as in, the FiveM team) did, but not when guiding you, the reader, through this wondrously written marvel of a guide. That means it must come from somewhere else!
As you can see, the first argument is the command name. The second argument is a function that is the command handler, and the third argument is a boolean that specifies whether or not it should be a restricted command.
The function itself gets an argument that is the source, which only really matters if you're running on the server (it'll be the client ID of the player that entered the command, a really useful thing to have), and an array of args which are basically what you enter after the command like /car zentorno making args end up being 'zentorno' or /car zentorno unused being 'zentorno', 'unused' .
Let's restart the resource and see what happens. Run restart mymode, then in the client chat box (default T) type /car zentorno. You'll see the chat box complain that you were too lazy to implement this. We'll show them that you're absolutely not lazy, and actually implement this now.
Then, we check if the vehicle is in the CD image using IS_MODEL_IN_CDIMAGE. This basically means 'is this registered with the game'. We also check if it's a vehicle using IS_MODEL_A_VEHICLE. If either check fails, we tell the player and return from the command.
Now, we call REQUEST_MODEL to load the actual vehicle model. This native takes a Hash argument, but in Lua you can also just pass a string and it'll be converted to a hash. You'll often see people use GetHashKey (GET_HASH_KEY), but if the native is specified as taking a Hash, you actually don't need this.
We loop calls to HAS_MODEL_LOADED to check if loading succeeded. Since this is a loop and we're cooperatively multitasked, you'll have to give the game time to run as well - otherwise it'll never even finish loading and the game will unfortunately freeze. That's what the Wait call is for - it waits for the specified amount of milliseconds, then returns right back into the script.
Players' physical incarnations are identified by their ped, which is short for 'pedestrian'. This is a GTA term, and it usually means 'anything that lives and has legs'. We use PLAYER_PED_ID to get the local (basically, whoever is executing this command) player's ped.
After we have the ped and store it in a variable, we get the position of the player ped using GET_ENTITY_COORDS. Since a ped is an entity (the same goes for vehicles and a few other things), this native is used for getting their position. This native, again, returns a Vector3, similar to how the spawnPos was defined earlier.
We use CREATE_VEHICLE to, well, create a vehicle. In the meanwhile, we snuck in a call to get the player's heading using GET_ENTITY_HEADING, which makes the car spawn facing the same direction as the player.
The true, false is a convention in entity creation natives to create the vehicle with a network object (true), but not make it a mission object (false). You usually want the former, or nobody else will see the vehicle - and you won't want the latter, since you're not writing a full R* mission script.
The game likes it when you clean up after yourself, and as we're not doing anything with the vehicle or the model anymore in this script, we'll let the game manage it. This is what we use SET_ENTITY_AS_NO_LONGER_NEEDED and SET_MODEL_AS_NO_LONGER_NEEDED for.
In your server console, refresh; restart mymode (yeah you can split stuff with semicolons), and try /car voltic2 in the game client (which should by now be really bored of respawning). You'll now have your very own Rocket Voltic!
1. Comprehensive Collection: From vehicles to scripts, eup to maps, the FiveM Store caters to every server need. We are the hub where top-quality mods, eup, launcher, and even specific server packs converge, ensuring that every server, whether roleplay or free roam, gleams with professionalism and intrigue.
5. Transparency at Its Best: We champion the cause ofopen-source. Say goodbye to encrypted codes and restricted access. Everydownload from our store is fully open, granting you the freedom to modify,adapt, and reimagine every line of code. Unlike other platforms like Tebex, webelieve in complete transparency and control for our customers.
6. Handcrafted Originals: Beyond the verified andoptimized scripts, we have a treasure trove of handcrafted originals,especially tailored for ESX, Qbus, and VRP frameworks. These unique scripts arethe result of countless hours of dedication, ensuring your server offers anexperience like no other.
All Fivem Scripts and MLO in our store is open source, all scripts have 2 versions QBCORE and ESX. After payment in couple min you will receive download link in your email. Please before you write in support, check FAQ page and important.txt file in resource folder.
EDIT:I see you're using the C Api of Lua and not a Lua binary to execute your scripts. Using the programming api you should be able the get your required result by executing A.lua and B.lua using the same lua-state (most commonly stored in the C-Variable "L").
You should get 2, because the require chain works from top-bottom, if you just run each file a time lua will execute just this little uniq execution, there is no persistency acros diferent executions, so you need to require as needed
There is no dedicated function to require Lua modules from the C-API.[1] So I oriented myself on the function dolibrary in the Lua interpreter to implement require which simply calls the Lua require function from C.
N.B.: I don't recommend having modules communicate by sharing global variables, especially when the order of loading the modules matters as is the case here. Better provide methods A.update_num(old) and B.update_num(old) which take the old value of num as an argument and return the updated value.
Start by learning the basics of the Lua programming language. This includes concepts like variables, data types, loops, and control structures. There are many tutorials and resources available online that can help you get started with this.
Join online communities and forums related to FiveM and Lua development. There are many active forums and communities where you can ask questions, share your scripts, and get feedback from other developers. This can be a great way to learn from others and get support as you continue to learn and improve your skills.
Super easy to use, and awesome to look at. Your players will love the immersive experience with both physical garage interiors as well as via a menu. See vehicle thumbnail previews, impound status, rename your vehicles, search, transfer vehicles to your friends and so much more.
3a8082e126