CREATE_MISSION_TRAIN – How to properly set up this native for drivable trains and subways?

Hello everyone!

I’m currently trying to implement a system on my FiveM server where trains (or subways) can travel along a predefined route, stop at designated stations, wait for a short while, and then depart again. I’ve come across the native CREATE_MISSION_TRAIN, but I’m having trouble understanding how it works and keep experiencing crashes. I’d really appreciate any guidance or clarifications you can provide.

Here are some specific questions I have:

  1. Functionality: Can CREATE_MISSION_TRAIN actually be used to create a fully functioning train/subway that stops at stations, waits, and continues on a route?
  2. Parameters: Which parameters (like train type, direction, etc.) do I need to pass to this native so it doesn’t crash my server? The documentation is a bit vague, and I’m not sure if I’m using the correct values.
  3. Control & Automation: Do I need to handle speed, stopping, and restarting manually in scripts, or is there a way to automate this behavior through the native (or other related functions)?
  4. Crashes: Why does CREATE_MISSION_TRAIN often cause crashes? Could it be from incorrect route IDs, the wrong train model, or a missing data setup?

If anyone has implemented trains or subways using this native and can share some insights, code snippets, or links to working examples, I’d be incredibly grateful. Thanks in advance for your help and any tips you can offer!

Best regards!

If you want to drive the trains that you create, or keep track of them in any way, then that is gonna take some coding, but it’s possible. There are some (old) resources on the forum that show you how to actually use them. I am not sure if the train natives work server-side yet, but that was in the works as well.

If you just want non-drivable, but enterable, ambient trains and subways (like the regular ones in GTA Online), use can use something like this:

--lua, client-side. You can just call those natives once.
    SwitchTrainTrack(0, true) -- Setting the Main train track(s) around LS and towards Sandy Shores active
    SwitchTrainTrack(3, true) -- Setting the Metro tracks active
    SetTrainTrackSpawnFrequency(0, 80000) -- The Train spawn frequency set for the game engine, in miliseconds
    SetTrainTrackSpawnFrequency(3, 80000) -- The Metro spawn frequency set for the game engine, in miliseconds
    SetRandomTrains(true) -- Telling the game we want to use randomly spawned trains
1 Like

This is fixed with tweak(extra-natives/five): sanitize mission train creation · citizenfx/fivem@4858e5b · GitHub