Adding random AI Trains

When adding random AI trains to the map with the following code, it seems to spawn the trains poorly like solo train cars going around, 3-4 of the same cars attached without a lead car. Train cars being left on the tracks not moving, etc.

Citizen.CreateThread(function()
while true do
Wait(0)
SwitchTrainTrack(0, true)
SwitchTrainTrack(3, true)
N_0x21973bbf8d17edfa(0, 120000)
SetRandomTrains(1)
end
end)

Anyone know why? Or how to fix it?

I do not know the natives at all, so this is to be taken with a spoon of salt…
That being said it buggers me that this is set to run on a while true - loop, even more so on a Wait(0).

Have you tried executing the code just once i.e. delete the while-loop and possily the Thread?

Citizen.CreateThread(function()
SwitchTrainTrack(0, true)
SwitchTrainTrack(3, true)
N_0x21973bbf8d17edfa(0, 120000)
SetRandomTrains(1)
end)

So something like this instead? I’m not very good with code tbh

I guess. Again, I don’t know the natives so can only assume and use trial and error.
Also look up your native N_0x2... and see what the two numbers do. It’s described in the Fivem native docs so you can try out different values.