Jump to content

[Question] Trucker Job (need help)


kieran

Recommended Posts

I have made another half working half failed script xD basically it is a trucker script and sort of works, but I can't figure out how to delete the blip attached to marker, the marker itself, and I also want to spawn the marker when I spawn the truck......   The only problem is I can't figure out how to create marker from eventhandler or call it from a function when it is created.

Here's what I have so far...

 

startdelmark = createMarker(  211.39999389648, 1809.5, 16.60000038147, "cylinder", 3, 42, 214, 153, 100 ) --spawns truck from here.
delivmark = createMarker(-1355.6999511719, -506.20001220703, 13.10000038147, "cylinder", 3, 42, 214, 153, 100) --this is the location.


TruckVehiclesTable = {}
local myBlip = createBlipAttachedTo ( startdelmark, 51 ) --blip on the spawn marker.


function spawntruck (truckElement)
	if ( isElement(truckElement) and getElementType(truckElement) == 'player' ) then 	 	
		local truckVeh = getPedOccupiedVehicle(truckElement)
		local x, y, z = getElementPosition (truckElement)
		local rotX, rotY, rotZ = getElementRotation (truckElement)
		TruckVehiclesTable[truckElement] = createVehicle (515, x-5, y, z+2)
		setElementRotation (TruckVehiclesTable[truckElement], rotX, rotY, 90)
		warpPedIntoVehicle (truckElement, TruckVehiclesTable[truckElement])
		local deliverBlip = createBlipAttachedTo ( delivmark, 52 ) --the blip I want on the destination marker.
		setBlipSize ( myBlip, 1 )
		outputChatBox ("You have took on the role of goods courier, deliver the goods to the dollar blip to get your reward", truckElement, 0, 255, 0)
		outputChatBox ("Do NOT leave the vehicle or it will be destroyed and your mission will fail!", truckElement, 255, 0, 0)
	end
end
addEventHandler("onMarkerHit", startdelmark, spawntruck)


function deliveredgood (truckElement) --the markers function I want to do when a player drives into it with the spawned vehicle.
	if ( isElement(truckElement) and getElementType(truckElement) == 'player' ) then 	 	
		local truckVeh = getPedOccupiedVehicle(truckElement)
		if ( truckVeh ) then 
			if ( TruckVehiclesTable[truckElement] ) then 
				destroyElement(truckVeh)
				TruckVehiclesTable[truckElement] = nil
				givePlayerMoney ( truckElement, tonumber(1000) )
				outputChatBox ("Congrats!  Here's your payment.", truckElement, 0, 255, 0)
				destroyElement(deliverBlip) -- I want to delete the blip attached to the marker below.
				destroyElement(delivmark) -- I want to delete it.
			end 
		end 
	en
end
addEventHandler("onMarkerHit", delivmark, deliveredgood)


addEventHandler ( "onPlayerVehicleExit", getRootElement(), function(truckVehicle, leftSeat, jackerPlayer)
	if ( TruckVehiclesTable[source] ) then 
		destroyElement(TruckVehiclesTable[source])
		TruckVehiclesTable[source] = nil 
		outputChatBox ("Your vehicle was destroyed as you left it and now your mission has failed!", truckElement, 255, 0, 0)
	end 
end)
	
	
addEventHandler('onPlayerQuit',root,
function ()
	if ( TruckVehiclesTable[source] ) then 
		destroyElement(TruckVehiclesTable[source]) 
		TruckVehiclesTable[source] = nil
	end 
end)

 

I also want to attach a trailer when the vehicle spawns, but it spawns on a table, so how would I do that, is there a createVehicleFromTable function or something s that?

Edited by kieran
Bad title.
Link to comment

The reason you cannot destroy the blip that's attached to the destination marker is that that blip is defined as local in another function, simply remove local and you'll be able to delete it.

Similar goes for the marker, if you want it spawned once the truck is spawned, simply make the marker spawn once that function is triggered, here's an example:

startdelmark = createMarker(  211.39999389648, 1809.5, 16.60000038147, "cylinder", 3, 42, 214, 153, 100 ) --spawns truck from here.
local myBlip = createBlipAttachedTo ( startdelmark, 51 ) --blip on the spawn marker.

delivmark = nil
local deliverBlip = nil

TruckVehiclesTable = {}

function spawntruck (truckElement)
	if ( isElement(truckElement) and getElementType(truckElement) == 'player' ) then 	 	
		local truckVeh = getPedOccupiedVehicle(truckElement)
		local x, y, z = getElementPosition (truckElement)
		local rotX, rotY, rotZ = getElementRotation (truckElement)
		TruckVehiclesTable[truckElement] = createVehicle (515, x-5, y, z+2)
		setElementRotation (TruckVehiclesTable[truckElement], rotX, rotY, 90)
		warpPedIntoVehicle (truckElement, TruckVehiclesTable[truckElement])
    	delivmark = createMarker(-1355.6999511719, -506.20001220703, 13.10000038147, "cylinder", 3, 42, 214, 153, 100) --this is the location.
		deliverBlip = createBlipAttachedTo ( delivmark, 52 ) --the blip I want on the destination marker.
		setBlipSize ( myBlip, 1 )
		outputChatBox ("You have took on the role of goods courier, deliver the goods to the dollar blip to get your reward", truckElement, 0, 255, 0)
		outputChatBox ("Do NOT leave the vehicle or it will be destroyed and your mission will fail!", truckElement, 255, 0, 0)
	end
end
addEventHandler("onMarkerHit", startdelmark, spawntruck)

 

You'll need to be more detailed about your last question, in what table are the trailers, how do you want them to spawn etc.

 

Hope I helped.

  • Like 1
Link to comment

It still doesn't, I also need the function to givePlayerCash etc from line 26 when a player hits the marker, it was creating the marker before, but the problem is it won't see the marker when the resource starts, meaning it gives me an error because the function linked to the destination marker apparently isn't there......   For some reason it only wants createMarker to be at the top. 

Also with trailers I want to make it simple and use the attachTrailerToVehicle function

newvehicle = createVehicle ( 515, 0, 0, 4 ) -- create a trailer tower (roadtrain)
setTimer(
	function()
		trailer = createVehicle ( 435, 0, 0, 4 )    -- create a trailer
		attachTrailerToVehicle ( newvehicle, trailer )   -- attach them
	end, 50, 1)

 

Link to comment

Okay, forget the marker etc, I fixed it, thanks to MoPoMaN's script I found out I needed to change

    	delivmark = createMarker(-1355.6999511719, -506.20001220703, 13.10000038147, "cylinder", 3, 42, 214, 153, 100) --this is the location.

to

    	delivmark = createMarker(-1355.6999511719, -506.20001220703, 13.10000038147, "cylinder", 3, 42, 214, 153, 100) --this is the location.
		addEventHandler ("onMarkerHit", delivmark, deliveredgood)

(I guess this makes it then tells it to go to the function, rather than trying to call nil from the script)

Now I just want to know how to attach trailers to a vehicle created on a table, other stuff is easy to figure out. :D 

Link to comment

Here's an example:

local x, y, z = getElementPosition(TruckVehiclesTable[truckElement])
trailer = createVehicle(515, x, y, z)
attachTrailerToVehicle(TruckVehiclesTable[truckElement], trailer)

I suggest you also put created trailer into a table so you can easily delete it.

  • Like 1
Link to comment
8 hours ago, kieran said:

Thanks, I'm sort of swaying off topic here, but is there a way to check if an element in a table is spawned and if it is destroy it  with destroyelement?

You can use:
 

if TruckVehiclesTable[truckElement] and isElement(TruckVehiclesTable[truckElement]) and getElementType(TruckVehiclesTable[truckElement]) == "vehicle" then
  destroyElement(TruckVehiclesTable[truckElement])
end

 

Link to comment
7 hours ago, Hale said:

You can use:
 


if TruckVehiclesTable[truckElement] and isElement(TruckVehiclesTable[truckElement]) and getElementType(TruckVehiclesTable[truckElement]) == "vehicle" then  destroyElement(TruckVehiclesTable[truckElement])end

 

I mean to check if  it a player has spawned a vehicle from the table, so when he hits the marker to spawn a truck/trailer any existing vehicles will be destroyed.

Link to comment
mybool = false
function spawntruck (truckElement)
	if ( isElement(truckElement) and getElementType(truckElement) == 'player' ) then 
if mybool == true then
destroyElement(TruckVehiclesTable[truckElement])
end	 	
		local truckVeh = getPedOccupiedVehicle(truckElement)
        mybool = true
		local x, y, z = getElementPosition (truckElement)
		local rotX, rotY, rotZ = getElementRotation (truckElement)
		TruckVehiclesTable[truckElement] = createVehicle (515, x-5, y, z+2)
		setElementRotation (TruckVehiclesTable[truckElement], rotX, rotY, 90)
		warpPedIntoVehicle (truckElement, TruckVehiclesTable[truckElement])
    	delivmark = createMarker(-1355.6999511719, -506.20001220703, 13.10000038147, "cylinder", 3, 42, 214, 153, 100) --this is the location.
		deliverBlip = createBlipAttachedTo ( delivmark, 52 ) --the blip I want on the destination marker.
		setBlipSize ( myBlip, 1 )
		outputChatBox ("You have took on the role of goods courier, deliver the goods to the dollar blip to get your reward", truckElement, 0, 255, 0)
		outputChatBox ("Do NOT leave the vehicle or it will be destroyed and your mission will fail!", truckElement, 255, 0, 0)
	end
end
addEventHandler("onMarkerHit", startdelmark, spawntruck)

make sure to set mybool to false when you destroy it .

Edited by 3laa33
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...