Jump to content

[HELP] Train Script


Recommended Posts

Hi guys,

i've made a Train script and it works but I have 1 little problem and that is timing.

The script does things in the wrong order.

What needs to happen is;

• on Destination Marker hit

> give player 1000$

> setPlayerPosition and Rotation

> Delete Marker and Train

What actually happens is;

> gives the money

> deletes the train and marker

> no setPlayerPosition

Here's the code;

• Server

function startTrainJob () 
    local thePlayer = source 
    trainVehicle = createVehicle(538, -1947.7, 140.8, 27.4) 
    --setElementAlpha(trainVehicle, 150) 
             
            function jobsDoneServer() 
                destroyElement(trainVehicle) 
                givePlayerMoney(thePlayer, 1000) 
                triggerClientEvent("destroyDestinationMarker", thePlayer) 
                setElementPosition(thePlayer, -1964.3, 138, 27.7) 
                setElementRotation(thePlayer, 0, 0, 90) 
            end 
             
        addEvent("onTrainJobFinish", true) 
        addEventHandler("onTrainJobFinish", root, jobsDoneServer) 
    if getElementType ( thePlayer ) == "player" then 
        warpPedIntoVehicle(thePlayer, trainVehicle) 
        triggerClientEvent("onStartTrain", thePlayer) 
    else 
        outputChatBox("Syntax Error!", thePlayer, 255, 100, 0) 
    end 
  
            function onExitTrain() 
                setElementPosition(thePlayer, -1964.3, 138, 27.7) 
                setElementRotation(thePlayer, 0, 0, 90) 
                destroyElement(trainVehicle) 
                triggerClientEvent("destroyDestinationMarker", thePlayer) 
            end 
  
    addEventHandler("onVehicleExit", trainVehicle, onExitTrain) 
     
end 
  
addEvent("startTrainJob", true) 
addEventHandler("startTrainJob", root, startTrainJob) 

• Client

GUIEditor = { 
    memo = {}, 
    button = {}, 
    window = {}, 
} 
  
addEventHandler("onClientResourceStart", resourceRoot, 
    function() 
        slantFont = guiCreateFont( "slant.ttf", 15 ) 
        trainGUI = guiCreateWindow(0.29, 0.24, 0.42, 0.49, "Train Job", true) 
        guiWindowSetSizable(trainGUI, false) 
        guiSetVisible(trainGUI, false) 
  
        trainStartButton = guiCreateButton(0.60, 0.20, 0.34, 0.17, "Start Job!", true, trainGUI) 
        guiSetFont(trainStartButton, slantFont) 
        guiSetProperty(trainStartButton, "NormalTextColour", "FFAAAAAA") 
        addEventHandler("onClientGUIClick", trainStartButton, trainWindowClose, false) 
        addEventHandler("onClientGUIClick", trainStartButton, triggerTrainServer, false) 
         
        trainCloseButton = guiCreateButton(0.67, 0.61, 0.20, 0.16, "Close", true, trainGUI) 
        guiSetFont(trainCloseButton, slantFont) 
        guiSetProperty(trainCloseButton, "NormalTextColour", "FFAAAAAA")  
        addEventHandler("onClientGUIClick", trainCloseButton, trainWindowClose, false) 
         
        trainGUIMemo = guiCreateMemo(0.08, 0.20, 0.41, 0.67, "Press the 'start job!' button to start the Train Job", true, trainGUI) 
        guiMemoSetReadOnly(trainGUIMemo, true) 
    end 
) 
  
trainMarker = createMarker(-1958.3, 137.89999, 26.7, 'cylinder', 2.5, 0, 100, 255, 255) 
  
function trainWindowOpen(hitElement) 
 if getElementType(hitElement) == "player" and (hitElement == localPlayer) then 
 guiSetVisible(trainGUI, true) 
 showCursor( true ) 
 end 
 end 
 addEventHandler("onClientMarkerHit", trainMarker, trainWindowOpen)  
  
function trainWindowClose() 
    guiSetVisible (trainGUI, false ) 
    showCursor ( false ) 
end 
  
function triggerTrainServer() 
    triggerServerEvent("startTrainJob", localPlayer ) 
end 
  
function startTrain() 
    destinationMarker = createMarker(1436.6, 2634.2, 9.8, 'cylinder', 7, 0, 100, 255, 255) 
        function jobsDone() 
            triggerServerEvent("onTrainJobFinish", localPlayer) 
        end 
        addEventHandler("onClientMarkerHit", destinationMarker, jobsDone) 
         
        function destroyDestinationMarker() 
            destroyElement(destinationMarker) 
        end 
         
        addEvent("destroyDestinationMarker", true) 
        addEventHandler("destroyDestinationMarker", root, destroyDestinationMarker) 
end 
  
addEvent("onStartTrain", true) 
addEventHandler("onStartTrain", root, startTrain) 

Thanks in advance and tell me how I can know which comes first and how I can time things right.

Regards ViRuZ

Link to comment

i, worked on fix it , for 3 hours : (

---ServerSide ,

addEvent('startTrainJob', true) 
addEventHandler('startTrainJob', root, 
function () 
trainVehicle = createVehicle(538, -1947.7, 140.8, 27.4) 
warpPedIntoVehicle(source, trainVehicle ) 
triggerClientEvent('onStartTrain', source) 
end 
) 
addEvent('onTrainJobFinish', true) 
addEventHandler('onTrainJobFinish', root, 
function () 
 destroyElement(trainVehicle) 
 setElementPosition(source, -1964.3, 138, 27.7) 
 setElementRotation(source, 0, 0, 90) 
 outputChatBox('You Made it and got $1000', source, 255,0,0) 
 givePlayerMoney(source, 1000) 
 triggerClientEvent("destroyDestinationMarker", source) 
 end 
 ) 
addEventHandler("onVehicleExit", trainVehicle, 
function () 
triggerClientEvent("destroyDestinationMarker", source) 
setElementPosition(source, -1964.3, 138, 27.7) 
setElementRotation(source, 0, 0, 90) 
 end 
 ) 

---ClientSide

GUIEditor = { 
    memo = {}, 
    button = {}, 
    window = {}, 
} 
  
addEventHandler("onClientResourceStart", resourceRoot, 
    function() 
        slantFont = guiCreateFont( "slant.ttf", 15 ) 
        trainGUI = guiCreateWindow(0.29, 0.24, 0.42, 0.49, "Train Job", true) 
        guiWindowSetSizable(trainGUI, false) 
        guiSetVisible(trainGUI, false) 
        trainStartButton = guiCreateButton(0.60, 0.20, 0.34, 0.17, "Start Job!", true, trainGUI) 
        guiSetFont(trainStartButton, slantFont) 
        guiSetProperty(trainStartButton, "NormalTextColour", "FFAAAAAA") 
        trainCloseButton = guiCreateButton(0.67, 0.61, 0.20, 0.16, "Close", true, trainGUI) 
        guiSetFont(trainCloseButton, slantFont) 
        guiSetProperty(trainCloseButton, "NormalTextColour", "FFAAAAAA") 
        trainGUIMemo = guiCreateMemo(0.08, 0.20, 0.41, 0.67, "Press the 'start job!' button to start the Train Job", true, trainGUI) 
        guiMemoSetReadOnly(trainGUIMemo, true) 
    end 
) 
  
trainMarker = createMarker(-1958.3, 137.89999, 26.7, 'cylinder', 2.5, 0, 100, 255, 255) 
  
addEventHandler('onClientMarkerHit', root, 
function (hitElement) 
if getElementType(hitElement) == "player" then 
guiSetVisible(trainGUI, true) 
 showCursor( true ) 
  end 
 end 
) 
  
  
addEventHandler('onClientMarkerLeave', root, 
function(leaveElement) 
if getElementType(leaveElement) == 'player' then 
    guiSetVisible (trainGUI, false ) 
    showCursor ( false ) 
   end 
  end 
 ) 
  
 addEventHandler('onClientGUIClick', root, 
 function () 
 if ( source == trainStartButton ) then 
 triggerServerEvent("startTrainJob", localPlayer) 
 guiSetVisible(trainGUI, false) 
 showCursor( false ) 
 elseif ( source == trainCloseButton ) then 
 guiSetVisible(trainGUU, false ) 
 showCursor (false) 
   end 
  end 
  ) 
   
addEvent('onStartTrain', true) 
addEventHandler('onStartTrain', root, 
function () 
destinationMarker = createMarker(1436.6, 2634.2, 9.8, 'cylinder', 7, 0, 100, 255, 255) 
outputChatBox('Go to your destination Marker ', localPlayer) 
end 
) 
  
addEventHandler('onClientMarkerHit', root, 
function(hitElement) 
if getElementType(hitElement) == "vehicle" then --- if it was vehicle hit the marker then 
triggerServerEvent("onTrainJobFinish", hitElement) 
  end 
 end 
 ) 
  
addEvent('destroyDestinationMarker', true) 
addEventHandler('destroyDestinationMarker', root, 
function () 
destroyElement(destinationMarker) 
end 
)  

Next time , try to organize your codes , because i have seen alot of repeating on some functions . . ,

/ use NP++ and save the code with UTF8- Without Bom so it can work on the server correct ,

Link to comment

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...