Jump to content

Problem with a train script


Aron800

Recommended Posts

Posted

Hello,

I want to make a train in my server but it doesn't work..

My code in: train_server.lua

engineer=createPed(227,1479,2669,3)
train=createVehicle(537,1479,2669,3)
setTrainDerailable(train, false)
warpPedIntoVehicle(engineer,train)
triggerClientEvent ( "driveTrain", getRootElement())

My code in: train_client.lua

function driveTrain()
Engineer=getVehicleOccupant(getPedOccupiedVehicle(getLocalPlayer()))
setPedControlState(Engineer,"accelerate",true)
end
addEvent( "driveTrain", true )
addEventHandler( "driveTrain", getRootElement(), driveTrain )

The Ped will come in the train with this script but the train doesn't go.

So does anyone know what's wrong with this script

Aron

Posted

Are you a passenger in the train of which the ped should drive? In your client-side script you are declaring the Engineer as the driver of the vehicle you're in.

Posted

Do you get any error/warning messages? I'm pretty sure you do about event not being added on client's side. I would NOT recommend creating stuff outside a function. Create ped and train inside onResourceStart event and/or triggerClientEvent after player has downloaded the resource.

Posted

Try this:

Server:

function createTrainAndEngineer()
   engineer=createPed(227,1479,2669,3)
setElementID(engineer,"engineer")
   train=createVehicle(537,1479,2669,3)
setTrainDerailable(train, false)
warpPedIntoVehicle(engineer,train)
end
addEventHandler("onResourceStart",getResourceRootElement(),createTrainAndEngineer)

Client:

function driveTrain()
   engineer=getElementByID("engineer")
setPedControlState(engineer,"accelerate",true)
end
addEventHandler("onClientResourceStart",getResourceRootElement(),driveTrain)

As you can see, the server creates train and engineer when the resource has started, not when it's starting. Engineer element has ID "engineer" which is used by client to get the element.

Posted

It still doesn't work but i found what is wrong

setPedControlState(engineer,"accelerate",true)

When i try to run the code with runcode it returns:

Aron executed command: setPedControlState(engineer,"accelerate",true)

Error: [string "return setPedControlState(engineer,"acceler..."]:1: attempt to call global 'setPedControlState' (a nil value)

But i dont know how i can make this work.

That setPedControlState works if i make a pilot so maybe it does not work on a train?

Posted

The only thing you found out on runcode is that setPedControlState is a clientside-only function. Yes, setPedControlState doesn't work on trains. It only works on peds. But engineer is a ped, not a train :)

Posted

I would suggest you leave this script somewhere in your mind only.. Trains have never worked properly and they have to be synced by at least 1 player.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...