Jump to content

[Question] Destroy vehicle on marker hit?


kieran

Recommended Posts

Hello, I have made a basic script to spawn a vehicle on marker hit, but I want to destroy the vehicle when a player drives back into the marker, my script is below, get message saying "Expected element at argument 1, got nil" (From line 14)

vehSpawnMark = createMarker (2410.6, 1552.1, 10, "cylinder", 5, 0, 255, 203, 100 )

function spawnvehicle (thePlayer)
	local x, y, z = getElementPosition (thePlayer)
	local mainveh = createVehicle (411, x-10, y, z)
	local rotX, rotY, rotZ = getElementRotation (thePlayer)
	setElementRotation (mainveh, rotX, rotY, 90)
	warpPedIntoVehicle (thePlayer, mainveh)
	outputChatBox ("You successfully spawned an Infernus!", thePlayer, 0, 255, 0)
end
addEventHandler("onMarkerHit", vehSpawnMark, spawnvehicle)

function removecar ()
	destroyElement (mainveh)
end
addEventHandler ( "onMarkerHit", vehSpawnMark, removecar )

It spawns the vehicle, but it doesn't see an element at "destroyElement (mainveh)" (line 14) when I hit the marker... :/ 

Got this script from a YouTube video, it was originally meant to spawn car on command and destroy it when a player left the car, but I changed it for markers.

Edited by kieran
Link to comment
  • Moderators
vehSpawnMark = createMarker (x,y,z, "cylinder", 5, 0, 255, 203, 100 )
VehiclesTable = {}

function spawnvehicle (thePlayer)
	if ( getElementType(thePlayer) == 'player' ) then 	
		local theVeh = getPedOccupiedVehicle(thePlayer)
		if ( theVeh ) then 
			if ( VehiclesTable[thePlayer] ) then 
				destroyElement(theVeh)
				VehiclesTable[thePlayer] = nil
				outputChatBox ("Your vehicle has been destroyed because you touched this marker", thePlayer, 0, 255, 0)
				return -- If u wanna give you a new vehicle after destroyed, delete this line
			end 
		end 
		local x, y, z = getElementPosition (thePlayer)
		local rotX, rotY, rotZ = getElementRotation (thePlayer)
		VehiclesTable[thePlayer] = createVehicle (411, x-10, y, z)
		setElementRotation (VehiclesTable[thePlayer], rotX, rotY, 90)
		warpPedIntoVehicle (thePlayer, VehiclesTable[thePlayer])
		outputChatBox ("You successfully spawned an Infernus!", thePlayer, 0, 255, 0)
	end 
end
addEventHandler("onMarkerHit", vehSpawnMark, spawnvehicle)

addEventHandler('onPlayerQuit',root,
function ()
	if ( VehiclesTable[source] ) then -- If the player has a vehicle from this script.
		destroyElement(VehiclesTable[source]) -- Delete it.
		VehiclesTable[source] = nil -- Remove player value from the vehicles table.
	end 
end)

 

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

@!#NssoR_) OMG thanks, this works nice. :D but it still pops up with warning in console when it destroys element...

[2017-06-17 03:58:47] WARNING: Test\vehiclestuff.lua:5: Bad argument @ 'getElementType' [Expected element at argument 1] 

change the ThePlayer in hitElement all

Link to comment
4 hours ago, kieran said:

@!#NssoR_) OMG thanks, this works nice. :D but it still pops up with warning in console when it destroys element...

[2017-06-17 03:58:47] WARNING: Test\vehiclestuff.lua:5: Bad argument @ 'getElementType' [Expected element at argument 1] 

function spawnvehicle (thePlayer)
		local theVeh = getPedOccupiedVehicle(thePlayer)
		if ( theVeh ) then 
			if ( VehiclesTable[thePlayer] ) then 
				destroyElement(theVeh)
				VehiclesTable[thePlayer] = nil
				outputChatBox ("Your vehicle has been destroyed because you touched this marker", thePlayer, 0, 255, 0)
				return -- If u wanna give you a new vehicle after destroyed, delete this line
			end 
		end 
if ( getElementType(thePlayer) == 'player' ) then 
		local x, y, z = getElementPosition (thePlayer)
		local rotX, rotY, rotZ = getElementRotation (thePlayer)
		VehiclesTable[thePlayer] = createVehicle (411, x-10, y, z)
		setElementRotation (VehiclesTable[thePlayer], rotX, rotY, 90)
		warpPedIntoVehicle (thePlayer, VehiclesTable[thePlayer])
		outputChatBox ("You successfully spawned an Infernus!", thePlayer, 0, 255, 0)
	end 
end
addEventHandler("onMarkerHit", vehSpawnMark, spawnvehicle)

try this ^ the problem was that it checks if the element that hit the marker is a player while the player hit it while he was in vehicle
so the hitElement is vehicle .. not player

Edited by 3laa33
Link to comment
  • Moderators

Sorry for the delay in reply.

vehSpawnMark = createMarker (x,y,z, "cylinder", 5, 0, 255, 203, 100 )
VehiclesTable = {}

function spawnvehicle (hitElement)
	if ( isElement(hitElement) and getElementType(hitElement) == 'player' ) then 	
		local theVeh = getPedOccupiedVehicle(hitElement)
		if ( theVeh ) then 
			if ( VehiclesTable[hitElement] ) then 
				destroyElement(theVeh)
				VehiclesTable[hitElement] = nil
				outputChatBox ("Your vehicle has been destroyed because you touched this marker", hitElement, 0, 255, 0)
				return -- If u wanna give you a new vehicle after destroyed, delete this line
			end 
		end 
		local x, y, z = getElementPosition (hitElement)
		local rotX, rotY, rotZ = getElementRotation (hitElement)
		VehiclesTable[hitElement] = createVehicle (411, x-10, y, z)
		setElementRotation (VehiclesTable[hitElement], rotX, rotY, 90)
		warpPedIntoVehicle (hitElement, VehiclesTable[hitElement])
		outputChatBox ("You successfully spawned an Infernus!", hitElement, 0, 255, 0)
	end 
end
addEventHandler("onMarkerHit", vehSpawnMark, spawnvehicle)

addEventHandler('onPlayerQuit',root,
function ()
	if ( VehiclesTable[source] ) then -- If the player has a vehicle from this script.
		destroyElement(VehiclesTable[source]) -- Delete it.
		VehiclesTable[source] = nil -- Remove player value from the vehicles table.
	end 
end)

 

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