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

Imagine if you made it so that when they enter without a pass it would give them a wanted level, and/or make a 911 call lol!

Hey, if you don’t mind me asking, how do you add blips? Was it easy?

Yeah, it’s pretty easy.

function createStationBlip(name, x, y, z)
	local blip = AddBlipForCoord(x, y, z)

	SetBlipSprite (blip, 533)
	SetBlipDisplay(blip, 4)
	SetBlipScale  (blip, 1.0)
	SetBlipAsShortRange(blip, true)
	SetBlipColour (blip, 1)

	BeginTextCommandSetBlipName("STRING")
	AddTextComponentString(name)
	EndTextCommandSetBlipName(blip)
end

I created that function specifically for creating the metro station blips, but you can modify it how you like and even make a more generic function out of it. The X, Y, Z coords are the exact same on the map as they are in the world.

Here’s a link to all the blip codes: Blips - Cfx.re Docs

Some don’t show up though so not sure what’s up there.

That’s good idea. I may do that once I get some other ideas put in place for this thing.

1 Like

Man, so when I said everything was going ok, I hadn’t actually tested it with multiple players on the server…it starts to get all kinds of funky. I started rewriting it so it would work better in MP mode. It’s been a challenge that’s for sure.

Do you know how to turn off the metro train and only keep the freight train?

Do you know if there is a way at all to stop the freight train from going through the city?

There is, I’ve figured out quite a bit of stuff since I last posted.

Here is how you can turn off the engine created trains and random spawns:

	SwitchTrainTrack(0, false) -- Deactivate main trains
	SwitchTrainTrack(3, false) -- Setting the Metro tracks inactive
	SetTrainTrackSpawnFrequency(0, 0) -- The main train spawn frequency set for the game engine
	SetTrainTrackSpawnFrequency(3, 0) -- The Metro spawn frequency set for the game engine
	SetRandomTrains(false)	

Just found out this release doesn’t work with the latest mptuner build of GTA. Nooooo

Ahh man, that sucks.

Well, I did manage to get this whole thing re-written to work better in multiplayer, but I’m not completely convinced yet it will even work correctly due to how the game engine is handling the train objects in the first place. I’m seeing culling issues and other inconsistencies which has been pissing me off. I fixed quite a bit of the jankyness, but it’s just lead to some new jank.

Funny thing is though I did manage to optimize and cleanup the code a lot and got it so that if you don’t have a metro ticket and you are on the train too long you will get the police called on you. The train won’t leave the station until you leave it as well…Same if you have a wanted level and you aren’t allowing fugitives on the trains. Essentially all the original bits still work, but you can freely walk on and off the trains. The doors open and close as well as it enters/leaves the stations.

I just need to determine when your ticket invalidates because one thing you should be able to do is transfer trains. That’s something you can do in the real world…your ticket doesn’t become invalid until you leave through the turnstile …

But none of that really matters if the trains won’t stay in the world properly. :frowning:

1 Like

Sounds like you’re making good progress! The original dev has abandoned this project I imagine.

Maybe you will also be able to figure out the fatal crash that happens with the new tuners build its really making me sad its the only script that doesn’t work. Confirmed by others too.

[     76750] [b2372_GTAProce]             MainThrd/ __Lua_InvokeNative: execution failed: Error executing native 0x63c6cca8e68ae8c8 at address 0x7ff77017918b.
[     76750] [b2372_GTAProce]             MainThrd/ ^1SCRIPT ERROR: Execution of native 63c6cca8e68ae8c8 in script host failed: Error executing native 0x63c6cca8e68ae8c8 at address 0x7ff77017918b.^7
[     76750] [b2372_GTAProce]             MainThrd/ ^3> CreateMissionTrain^7 (^5CreateMissionTrain.lua^7:17)
[     76750] [b2372_GTAProce]             MainThrd/ ^3> handler^7 (^5@Trains/client/client.lua^7:251)

I believe that crash is being caused by the fact that there should be 2 extra params passed to it as mentioned here:

It doesn’t say what they are but it seems like they may be the same as the CreatePedInsideVehicle function where the last 2 params are IsNetwork and bScriptHostPed: CreatePedInsideVehicle - Natives @ Cfx.re Docs

If this doesn’t resolve it, I have some other ideas, but let me know if it does.

I’m so sad that it doesn’t work with tuners update and crashes people immediately.
How do I implement this idea of yours above? Sorry I’m new to the script stuff

i noticed there is also

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

Which does not exist in the model list in the docs

I removed that…
suddenly I’m not crashing… but to be determined! I hope its working now!

I also updated the Fx manifest version to the latest

update second time i loaded in it crashed me again but no clue why this time nothing in logs

[    853641] [b2372_GTAProce]             MainThrd/ FiveM-Trains: Train Models Loaded
[    853656] [b2372_GTAProce]                 7076/ ResourceCache::AddEntry: Saved cache:v1:e6c7a585acc220d85f7e361ef49263320914044b to the index cache.
[    853828] [b2372_GTAProce]                17404/ ResourceCache::AddEntry: Saved cache:v1:9460cd4807e5cca8e8a8c71875e6a95d98aea02b to the index cache.
[    853891] [b2372_GTAProce]             MainThrd/ FiveM-Trains: Train 1 created (Freight).
[    854016] [b2372_GTAProce]                 7076/ ResourceCache::AddEntry: Saved cache:v1:90aa08f536c0bb5e0799465a168bbcde6aac865f to the index cache.
[    854031] [b2372_GTAProce]                 7076/ ResourceCache::AddEntry: Saved cache:v1:f04e6c78237931382605fedbfaa4bdceae52fe7f to the index cache.
[    854047] [b2372_GTAProce]                 7076/ ResourceCache::AddEntry: Saved cache:v1:581f1ed2eb01883aec8bfc3e9ae7bf968acb04a5 to the index cache.
[    854047] [b2372_GTAProce]                 7076/ ResourceCache::AddEntry: Saved cache:v1:1ec6dd2457c8bc69923883de72a8af370672a1c0 to the index cache.
[    854047] [b2372_GTAProce]                17404/ ResourceCache::AddEntry: Saved cache:v1:e5d5963f9a316c57a630bb59477bb19338738ef3 to the index cache.
[    854063] [b2372_GTAProce]                 7076/ ResourceCache::AddEntry: Saved cache:v1:08aabff3164a1dd04502352364aac833133c3af4 to the index cache.
[    854125] [b2372_GTAProce]                17404/ ResourceCache::AddEntry: Saved cache:v1:59d8283c9eeb713c3f5a1a606b43f6a420d185cc to the index cache.
[    854141] [b2372_GTAProce]                 7076/ ResourceCache::AddEntry: Saved cache:v1:555c269957baef9e0e652567a8c9997d79e7090b to the index cache.
[    856891] [b2372_GTAProce]                17268/ Error: [md]# Disconnected by server
[    856891] [b2372_GTAProce]                17268/ Game crashed: FiveM_b2372_GTAProcess.exe!sub_1415FE51C (0x230)
1 Like

“But for many of you on the newest Los Santos Tuners update, this has broken your trains and left you with just one default train spawning. This patch will place an updated Trains.xml in the new location that Rockstar places it in since the newest DLC. This should have your Kitsune Trains mod working again normally. This is the very fist version of my patch so if it is not working for you, please be patient. I am trying to get all of the changes figured out and get my mod fully compatible again.” Interesting. From Kitsune Trains [OIV] - GTA5-Mods.com

Though this is for single player.

[    c-scripting-core] Creating script environments for Trains
[       script:Trains] Error loading script config.lua in resource Trains: @Trains/config.lua:46: attempt to call a nil value (global 'SwitchTrainTrack')
[       script:Trains] stack traceback:
[       script:Trains]  @Trains/config.lua:46: in main chunk
[    c-scripting-core] Failed to load script config.lua.

pretty common error i see a lot, is this normal

No, that’s not normal. It means that function doesn’t exist, which is odd, because it’s one of the engine functions. In fact, it’s one that I posted above. It’s how you enable/disable the train tracks in the game.

Just an update so far I have things working fairly well. Havent tried with more than 8 people yet on the server. I set randomized trains. Sometimes the metro train is in Blaine County rofl. Connected to nothing. I might just remove it entirely. I dont mind if Freight Trains go through the city. Sometimes they do in real life. Usually at night though. Also it will make some police pursuits interesting like you see in the movies where someone JUST makes it past a train and the cops get stuck LOL.

And that function can only be called from a client script…it looks like it’s being called from a config script which is probably also being called by a server script.

Yeah, just using the random trains sounds like it will work for what you want.

via waar kan ik jou contacteren willem ik wil iets zeggen tegen jou in prive

So…I have been able to work out most of the wonkiness regarding the trains in multiplayer, I’m just down to one last issue I’ve been trying to resolve for a few days now. Basically what’s happening is that the player who created the trains will lose control of them due to it being taken control of by another player and function calls like SetNetworkIdCanMigrate which says it can prevent a network id from being taken control of by another player just don’t seem to work.

However, I have managed to get it so the host can take back control of a train they have lost control of, but it’s not perfect yet as sometimes they don’t regain control and the train gets deleted. Which is also interesting because there is a function call which supposedly allows you to prevent the game engine from deleting an entity, but it also doesn’t seem to work. Either way, I’m getting close to these things working correctly in multiplayer.

I did add another feature though. When you arrive at a stop you will get notified which stop you’ve arrived at and at the top of the screen it will print what the next stop is as long as you are on a train.

Thats awesome!

For now I’ve managed to have two random freight trains going around the map as I’m not using Metros I commented out the stations.

I would love to make it so that the Train Blips show to everyone and not only the host though I have no clue how