[Release] TrainSportation ~ Drive & Be a Passenger in a train!

@Lex_The_Great I believe I sent the pull request

You put the file in the wrong folder! Put client.lua into the /client/ folder.

Please instead of drawtext, use something more native like

function HelpText(text)
  SetTextComponentFormat("STRING")
  AddTextComponentString(text)
  DisplayHelpTextFromStringLabel(0, 0, 0, -1)
end

along with using something like

HelpText("Press ~INPUT_DETONATE~ to spawn a ~g~train")

for the message, as you shouldnā€™t always believe that the key will be G

how can i install this script ?

2 Likes

Fantastic script @Lex_The_Great - Spent some time playing with how you spawn trains vs how I was doing it myself.

One thing I found fascinating is that if the owner of the train (the player that spawned it) gets off the train - it drops the train speed to 18 mph and I canā€™t seem to force it to go any faster.

(Keep in mind, I have it so the player is a passenger - not the driver.)

Might have something to do with the AI driver I suspect, as even forcing the train speed in real time will only temporarily increase it - and then it slows right back down.

This was only designed with getting into the train and being able to drive it in mind. Spawning trains such are just so you can test it. So having this enabled, and having another resource spawn trains for a mission, or randomly spawn them on the map. Then you can enter the trains other resources spawn!

The only way I found to spawn trains was this

function createTrain(type,x,y,z)
	local train = CreateMissionTrain(type,x,y,z,true)
	SetTrainSpeed(train,0)
	SetTrainCruiseSpeed(train,0)
	SetEntityAsMissionEntity(train, true, false)
	debugLog("createTrain.")
end

How did you do it? :o

The train gets taken over by the AI when you exit it. I could not find a way to disable this :confused:
The speed of the train is done by button presses and not setting the max speed. So when the AI takes over there isnā€™t much you can do about it other than getting back into it, or keeping track of the train and forcing it to go fasterā€¦

1 Like

As far as spawning trains - I just have an async thread in a standalone client script.

I define what the target coords are, what the models are (and call the hash) and then spawn them via CreateMissionTrain - and voila. They spawn, and go about their lives.

One thing I have noticed is that once a new player gets in, or the original player gets out - the entity ID changes (i believe) of the train - and it goes back to the default 18mph. Regardless of any natives called trying to force it back to the speed I want - including adjusting the target of SetTrainSpeed or SetTrainCruiseSpeed, or even trying to set the cruise speed that the ped (AI) in the drivers seat has.

Edit: And all of that occurs within the async thread.

Edit 2: I pulled my inspiration from the client script that fs_freemode uses:

local thisTrain
local trainCoords = {}
local storedTrains = {}

Citizen.CreateThread(function()
  local trainModels = {"freight", "freightcar", "freightgrain", "freightcont1", "freightcont2","tankercar", "freighttrailer", "metrotrain"}
  local trains = {
    {type=0, x=-498.4123, y=4304.3, z=88.40305},
    {type=0, x=2324.3, y=2670.7, z=44.45},
    {type=0, x=1063.595, y=3227.571, z=39.3899},
    {type=7, x=2928.826, y=3572.775, z=54.0699},
    {type=15, x=217.616, y=-2215.75, z=11.6666},
    {type=22, x=1800.49, y=3504.19, z=37.9},
    {type=24, x=181.1, y=-1198.8, z=37.6},
  }

  for i= 1, 8 do
    RequestModel(GetHashKey(trainModels[i]))
    while not HasModelLoaded(GetHashKey(trainModels[i])) do
      Citizen.Wait(1)
    end
  end

  RequestModel(GetHashKey("s_m_m_lsmetro_01"))
  while not HasModelLoaded(GetHashKey("s_m_m_lsmetro_01")) do
    Citizen.Wait(1)
  end

  for i=1, 7, 1 do
    local trainCoords = trains[i]
    local thisTrain = CreateMissionTrain(trainCoords.type, trainCoords.x, trainCoords.y, trainCoords.z, true)
    SetEntityProofs(thisTrain, true, true, true, true, true, false, 0, false)
    SetEntityAsMissionEntity(thisTrain, true, true)
    CreatePedInsideVehicle(thisTrain, 26, GetHashKey("s_m_m_lsmetro_01"), -1, 1, true)
    table.insert(storedTrains, {train = thisTrain})
  end
end)

@Lex_The_Great

I did run a test on tram time in GTA Online from Little Seoul to the Strawberry/VU station. Seems to be at 18/19 mph itself.

Going to test to see if the speed is higher in the tunnels.

Either way, it looks like something in GTA itself is declaring that tram speed and thus over riding.

1 Like

Do you have any trainers that disable AI vehicles?

how can i install this mod? i would like have some trains

Hey! When i do f next to a trolley, the doors will open automaticly. I canā€™t close them and players canā€™t use the doors. There is like an invisible barrier :confused:

Does this conflict with the other Train mod?

Would that be why I canā€™t see it?

Got it to work, has to be under main resource folder, canā€™t be consolidated. How does the horn work?

How would you install this script?

1 Like

can we add custom textures to the trains(replacement)?

I would assume it would be like modding any other vehicle?

Should be like any other resource!

Like the car horn I think. I will check to see if everything is working. (EDIT: Press E, just tried it :stuck_out_tongue:)

Yeah, Iā€™m not sure I can do anything about thatā€¦ Itā€™s not anything I put there.

Iā€™ve tried that and it doesnā€™t work (Iā€™ve made a resource like any other vehicle replacement resource) I really want to get replacement textures working so if anyone could help that would be awesome!( Iā€™ve already got lots of vehicles working so thatā€™s fine just canā€™t get train textures to replace)

Hi the script works great for me. But i wanna have a train driver (conducter) job for it. my coding skillz are still not good enough to do it. i learning by doing atm. So it would be great if anyone could help me to made such a script

I would love to, my fivem server doesnā€™t give me errors though so I havenā€™t been able to really make anything that works (canā€™t see what is wrong x.x). So I can help you out with what to do.

I would look at the spawn script inside TrainSportation. Use it in your conductor job. Next I guess would be creating a goal for the conductor. Really the only passenger train is the trolly, and that doesnā€™t work 100% so maybe a delivery job. It would be similar to mail job/drop of. I would suggest looking into something like that. You would just set the goal to be somewhere on the track. Trains go through each other so it should be ok to let. Multiple people do this at once, unless you want to limit it!