Jump to content

[Question] Set a players element data on vehicle explode


kieran

Recommended Posts

Made a trucker script, but I want to set the players data when there truck/trailer explodes, so I tried a for loop and the idea was to get all players element data and set it to false if the source was there vehicle...

function ExitVehicle(thePlayer) --This function works fine, is just here to give you an idea of what I'm trying ti achieve
	if getPlayerTeam(thePlayer) == (TruckerTeam) then
		local spawnedTruck = getElementData( thePlayer, "Trucker.truck" )
		local theTrailer = getElementData( thePlayer, "Trucker.trailer" ) --Name changed from spawnedTrailer to prevent conflict
		if (spawnedTruck) then 
			outputChatBox("You have 20 seconds to get back to your truck!", thePlayer)
			TruckTimer = setTimer(function()
					if ( spawnedTruck ~= false ) then --(~= is not equal)
						destroyElement(spawnedTruck)
						setElementData(thePlayer, "Trucker.truck", false)
					end
					if (theTrailer) ~= false then
						destroyElement(theTrailer)
						setElementData(thePlayer, "Trucker.trailer", false)
					end
					triggerClientEvent(thePlayer, "destroyMarker", thePlayer)
					triggerClientEvent(thePlayer, "shipmentMarker", thePlayer)
				end,20000,1)
		end
		if not (spawnedTruck) and (theTrailer) then
			outputChatBox("You have 20 seconds to get back to your truck!", thePlayer)
			TrailerTimer = setTimer(function()
				destroyElement(theTrailer)
				triggerClientEvent(thePlayer, "destroyMarker", thePlayer)
				setElementData(thePlayer, "Trucker.trailer", false)
				triggerClientEvent(thePlayer, "shipmentMarker", thePlayer)
			end,20000,1)
		end
	end
end

addEventHandler("onVehicleExit",getRootElement(),ExitVehicle)

--When vehicles explode the players data will be removed
function explodingTruck()
	for _,player in ipairs(getElementsByType("player")) do --LOOP through all players
		local spawnedTruck = getElementData( player, "Trucker.truck" ) --The data is the players vehicle, set when the player spawned it.
		local theTrailer = getElementData( player, "Trucker.trailer" ) --^^^
		if source == spawnedTruck then --If the source was the players vehicle 
			setElementData(player, "Trucker.truck", false) --The idea is to set there data to false so they can spawn another one if the server has automatic respawn/deletion for vehicles.
		end
		if source == theTrailer then
			setElementData(player, "Trucker.trailer", false)
			triggerClientEvent(player, "destroyMarker", player) --Obviously I want to destroy the players mission blip and marker if they have a trailer, but it fails to get the player.
		end
	end
end
addEventHandler("onVehicleExplode",getRootElement(),explodingTruck)

Full resource can be found here if you need more code: https://community.multitheftauto.com/?p=resources&s=details&id=15161

debugscript 3

When the timer to check for data (check if the player has a vehicle spawned) is done, the below shows, clearly showing me that it failed to set players data to false.

[2018-01-14 22:35:14] WARNING: RP-Trucker\Trucker_s.lua:188: Bad argument @ 'destroyElement' [Expected element at argument 1]
[2018-01-14 22:35:14] WARNING: RP-Trucker\Trucker_s.lua:192: Bad argument @ 'destroyElement' [Expected element at argument 1]

 

Any help with it would be nice, I want the community to have a perfect RP resource, plus it'll help me learn to make new ones ^.^  Thanks in advance!

Edited by kieran
Link to comment
  • Moderators

It clearly says that the truck isn't an element.

 

Use the isElement function to check if the value of the elementdata is still an element.

Because there are not elements saved in to the LUA memory. But reference to the elements are. Which means that if an element is the destroyed, the reference is not. So it keeps it's positive value. An if statement that only checks if the value is not or false will not work.

This is the only way of validating elements that exist longer in-game.

if isElement(variable) then

 

  • Thanks 1
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...