Desaster Posted October 26, 2013 Share Posted October 26, 2013 hello, this is my script and I you couldn't find how to slove the following problems - when two players take the train at the sametime if anyone of them stop at a station the other will stop too and that's bad -when the player quits the car the colCuboids won't be destroyed so every time the player run throught the colshape he must pay oney for nothing I tryed every thing but I can't find what I must do Server Side : function TrainlvTols(source, myTrain) if (getPlayerMoney (source) > 499) then local myTrain = createVehicle(537,1453.8831787109, 2628.9331054688, 11.392612457275) -- This will make a freight train just east of the LS train station driver = getVehicleOccupant ( myTrain ) setTrainDerailable(myTrain, false) -- myTrain can not be derailed now warpPedIntoVehicle(source, myTrain) -- This will warp you to inside the train setTrainSpeed(myTrain, 3) -- Set the train speed to 1 - 100mph, 160kmh for i,v in ipairs(trainStopPos) do thecol = createColCuboid(unpack(envy)) addEventHandler ( "onVehicleExit", myTrain, function(thecol) --- here the remove col shape destroyElement(thecol) destroyElement(myTrain) end ) addEventHandler ( "onColShapeHit", thecol, function(driver) setTrainSpeed(myTrain, 0) -- Set the train speed to 1 - 100mph, 160kmh takePlayerMoney ( driver, 250 ) if (getPlayerMoney (source) < 499) then destroyElement(myTrain) outputChatBox ( "You can't afford to go to the next station!", source, 255, 0, 0, true ) end setTimer ( function() setTrainSpeed(myTrain, 3) end, 5000, 1 ) end ) end addEventHandler ( "onVehicleExit", myTrain, function() destroyElement(myTrain) end ) else outputChatBox ( "You can't afford to go to the next station!", source, 255, 0, 0, true ) end end thnx for reading ps : there is also a client side but nothing important is in it Link to comment
tosfera Posted October 26, 2013 Share Posted October 26, 2013 Why didn't you just create seperate functions instead of everything in 1 function, and creating new functions inside the eventHandler. And why didn't you do alot of these things client sided? Would actually fix the biggest problems with it. ps. checking the code now. edit1; rewritten the code for you, in a client/server format. I did not test it! client; addEvent ( "removeCols", true ); addEventHandler ( "removeCols", getRootElement(), function () for i, c in ipairs ( getElementsByType ( "colshape" ) ) do local colOwner = getElementData ( c, "colowner" ); if ( colOwner == getPlayerName ( getLocalPlayer() ) ) then destroyElement ( c ); end end end ); addEvent ( "createTrainCols", true ); addEventHandler ( "createTrainCols", getRootElement(), function ( thePlayer ) if ( thePlayer == getLocalPlayer() ) then for i, c in ipairs ( trainStopPos ) do local col = createColCuboid ( unpack ( envy ) ); setElementData ( col, "colowner", getPlayerName ( getLocalPlayer() ) ); setElementData ( col, "trainStop", i ); createBlipAttachedTo ( col, 19 ); end end end ); addEventHandler("OnClientColShapeHit", getRootElement(), function ( thePlayer ) if ( thePlayer == getLocalPlayer() ) then local stop = getElementData ( source, "trainStop" ); if ( stop ) then setTrainSpeed ( getPedOccupiedVehicle ( getLocalPlayer() ), 0 ); --setTrainSpeed ( thePlayer, 0 ); -- not sure which one works. destroyElement ( source ); takePlayerMoney ( 500 ); if not ( getPlayerMoney () > 499 ) then triggerServerEvent ( "destroyTrain", getLocalPlayer(), getLocalPlayer() ); end setTimer ( function() setTrainSpeed(myTrain, 3) end, 5000, 1 ); end end end ); server; local startMarker = createMarker ( 0, 0, 3, "marker", 2, 255, 255, 255, 255 ); addEventHandler ( "onMarkerHit", startMarker, function ( hitElement ) local money = getPlayerMoney ( hitElement ); local playerName = getPlayerName ( hitElement ); if ( money > 499 ) then local myTrain = createVehicle ( 537, 1453.8831787109, 2628.9331054688, 11.392612457275 ); setElementData ( myTrain, "owner", playerName ); setTrainDerailable ( myTrain, false ); warpPedIntoVehicle ( hitElement, myTrain ); setTrainSpeed ( myTrain, 3 ); triggerClientEvent ( "createTrainCols", hitElement, hitElement ); else outputChatBox ( "You can't afford to go to the next station!", hitElement, 255, 0, 0, true ); end end ); addEvent ( "destroyTrain", true ); addEventHandler ( "destroyTrain", getRootElement(), function ( thePlayer ) local vehicle = getPedOccupiedVehicle ( thePlayer ); if ( vehicle ) then destroyElement ( vehicle ); end outputChatBox ( "You can't afford to go to the next station!", thePlayer, 255, 0, 0, true ); end ); addEventHandler ( "onVehicleStartExit", getRootElement(), function ( thePlayer ) local model = getElementModel ( source ); if ( model == 537 ) then local speed = getTrainSpeed ( getPedOccupiedVehicle ( thePlayer ) ); if not ( speed == 0 ) then return; end end end ); addEventHandler ( "onVehicleExit", getRootElement(), function ( thePlayer ) local model = getElementModel ( source ); if ( model == 537 ) then local owner = getElementData ( source, "owner" ); if ( owner == getPlayerName ( thePlayer ) ) then destroyElement ( source ); triggerClientEvent ( "removeCols", thePlayer ); end end end ); PS. It's not the best way to do it, it will work if your playerbase is low. Else there is a small chance on lag. Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now