[RELEASE] FiveM-Trains : Complete standalone AI trains system and enterable trains

Has there been any progress on making this work with onesyc infinity?

I tried using custom models, as i wanted to have 2 locomotives in front, with the second one flipped. I have my trains.xml setup like this:

<?xml version = "1.0" encoding = "UTF-8"?>
<train_configs version = "1">
<train_config
			name = "freight_config0"
			populate_train_dist = "40.0"
			announce_stations = "false"
			doors_beep = "false"
			carriages_hang = "false"
			carriages_swing = "false"
			link_tracks_with_adjacent_stations = "true"
			carriage_gap = "0.1">
			
			<carriage
					model_name = "freight"
					max_peds_per_carriage = "0"
					flip_model_dir = "false"
					do_interior_lights = "true"
					carriage_vert_offset = "1.65"
			repeat_count = "1" />
            <carriage
					model_name = "freight"
					max_peds_per_carriage = "0"
					flip_model_dir = "true"
					do_interior_lights = "true"
					carriage_vert_offset = "1.65"
			repeat_count = "1" />			
	</train_config>

<train_config_group name = "freight_group">
		<train_config_ref name = "freight_config0" />
</train_config_group>
</train_configs>

My fxmanifest looks like this:

files {

    'data/vehicles.meta',
    'data/trains.xml',
} 

data_file 'VEHICLE_METADATA_FILE' 'data/vehicles.meta'
data_file 'TRAINCONFIGS_FILE' 'data/trains.xml'

FiveM doesnt seem to register this, and keeps spawing the AI train with 1 locomotive. Any solutions to this?

1 Like

I canā€™t wait to get this on my server. Looks great

This is still sort of working (except the despawn bug) on 3539 and I decided to create an ESX tutorial, that should actually work, with the cost of removing the bank purchase function (only cash now), as you canā€™t check playerā€™s bank ballance properly as far as I know.

Credits to @Fezz for the original tutorial and creators of esx_vehicleshop, as I used a bit of code from it.

Here it is:

  1. Open the client.lua in the client folder and add the following to it:
local ESX = nil

Citizen.CreateThread(function()
    while ESX == nil do
        TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
        Citizen.Wait(0)
    end
end)
  1. Find
BankAmount = 10000    --StatGetInt("BANK_BALANCE",-1)
  1. Delete stuff from that till
IsPlayerUsingTicketMachine = false
  1. Replace it with this:
				if UserBankIDi == 1 then		  		-- Maze Bank
					BankIcon = "CHAR_BANK_MAZE"
					BankName = "Maze Bank"
				end
				
				if UserBankIDi == 2 then				-- Bank Of Liberty
					BankIcon = "CHAR_BANK_BOL"						
					BankName = "Bank Of Liberty"
				end

				if UserBankIDi == 3 then		  		-- Fleeca (Default Fallback to!)
					BankIcon = "CHAR_BANK_FLEECA"
					BankName = "Fleeca Bank"
				end
				
				ESX.TriggerServerCallback('FiveM-Trains:buyTicket', function(hasEnoughMoney)
					if hasEnoughMoney then
						SMS_Message("CHAR_LS_TOURIST_BOARD", Message[Language]['los_santos_transit'], Message[Language]['tourist_information'], Message[Language]['ticket_purchased'], true)
						PlayerHasMetroTicket = true
					else
						SMS_Message(BankIcon, BankName, Message[Language]['account_information'], Message[Language]['account_nomoney'], true)
					end
				end)

It should look like this now:

  1. Open server.lua in the server folder and add the following to it:
ESX = nil

TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)

ESX.RegisterServerCallback("FiveM-Trains:buyTicket", function(source, enoughmoney)
    local xPlayer = ESX.GetPlayerFromId(source)
    if xPlayer.getMoney() >= TicketPrice then
        xPlayer.removeMoney(TicketPrice)
		enoughmoney(true)
    else
        enoughmoney(false)
    end
end)
  1. Remove this line from config.lua (in the resource folder itself):
PayWithBank 			= 25		-- Change this to 1 if you want users to pay with bank card (NOTE: Do implement your OWN banking system here please!)

And thatā€™s it, tell me if there are any problems with it, Iā€™ll try to fix them if I can

1 Like

Can you please add onesync infinity compatibility?

6 Likes

trains and trans do not show up at all

Same here. Everything else works (tickets etc), but there is no trains.

Hello the script has many flaws. I have it runnning (and the first player becomes the host of the trains), but most of the time the train blips dont show. Also when another player gets close the train despawns and the train driver is left on the track. It most of time doesnā€™t work.

I am running a standalone server.

temp fixes

MetroTrain = CreateMissionTrain(25,40.2,-1201.3,31.0,true)

MetroTrain2 = CreateMissionTrain(25,-618.0,-1476.8,16.2,true)

they moved metro ahead 1 i assume for the new freightcar2

		tempmodel = GetHashKey("freightcar2")
		RequestModel(tempmodel)
		while not HasModelLoaded(tempmodel) do
			RequestModel(tempmodel)
			Citizen.Wait(0)
		end

the main train should be

Train = CreateMissionTrain(math.random(0,24), x,y,z,yesorno)

0-24 for all the diff freight train configs

Is it easy to disable money requirements, or will it work without a banking system?

Can confirm that it does work, although sometimes the trains donā€™t seem to spawn.

When my player loads it causes a crash with the following errorā€¦

FiveM-Trains: Train 1 created (Freight).
__Lua_InvokeNative: execution failed: Error executing native 0x63c6cca8e68ae8c8 at address 0x7ff7c9a7918b.
SCRIPT ERROR: Execution of native 63c6cca8e68ae8c8 in script host failed: Error executing native 0x63c6cca8e68ae8c8 at address 0x7ff7c9a7918b.
CreateMissionTrain (CreateMissionTrain.lua:17)
handler (@FiveM-Trains/client/client.lua:253)

Anyone had this issue?

Just had the same issue. I tried to find a way around with lua but I couldnā€™t so I experimented with onesync.

These settings in the server.cfg are working for me:

set onesync_enableInfinity 1
set onesync legacy
set onesync_forceMigration true
onesync_distanceCullVehicles true
set onesyncworkaround763185 1

Yup. Iā€™ve got the same thing happening. This script is severely outdated and clearly isnā€™t compatible with the latest FiveM version.

you got a fix yet?

1 Like

anyone have an updated versionā€¦ i mean i literally just want some freight trains in sandy going by and stuff. not even enterable

Downloaded this mod last night and itā€™s good. I have been adapting it to my needs and fixing some things. One thing I found is that you need to call

SetTrainsForceDoorsOpen(false)

This will prevent the doors from opening when a player enters the train. According to the docs the default behavior of trains in multiplayer it for the doors to be forced openā€¦very weird.

Something else I have done and Iā€™m still tweaking is made it so instead of only being able to spawn 2 metro trains that go in opposite directions you can now spawn as many as you want. They all spawn at the same starting location stopped. The first one created will start moving and then each one a minute after the next. This will create a constantly running metro train system and allow there to be trains going both ways.

The script still works? No wonky freight trains?

Yeah, itā€™s been working well. Not seen any wonky freight trains, but Iā€™ve only been working on the metros. Iā€™ll let you know though if I see anything weird.

I put it in and so far its nice. I was on a server a few months back that had it and the freight trains were spawning random cars by themselves in places and it was so jank the moving train would come and move right through it lol.

But it doesnt seem to be doing that so far on my server but the other server I saw this script on had about 100 players on at the time maybe it messes it up when there are so many people

I personally just want it for the train rolling through Blaine County as it looks nice. Not using it for riding or anything like that.

Ha, thatā€™s crazy. And yeah, the freight train does look nice running trough Blaine County. So far since I last posted Iā€™ve gotten the metro trains to open their doors upon stopping and closing them before leaving. You can walk right onto the trains. Iā€™m running 8 metro cars at the moment and they are working great. Iā€™m going to get it so instead of having to press E in order to get on the train with your pass, you can just walk on and if you donā€™t have a pass it will prevent you from entering. Should be a much smoother experience.

Oh last night I also added the map blips for the metro stations.

1 Like