Jump to content

[Question]How can you destroy one element in a table?


kieran

Recommended Posts

Hello, I have made a script that spawns vehicles, to avoid lag I have added the spawned vehicles to a table, I am trying to make it so if the data exists on the table, the vehicle for the player that spawned it only will be destroyed, the  problem is, ALL vehicles in the table are destroyed instead of just the players vehicle.

Here's my code, I thought of setting element data, but the only way I could think of was setting the players data to the license plate from the vehicle and then checking all vehicles, but this would create a load of lag if many people had vehicles spawned.

The other problem is, it is only triggered when you are in the vehicle, I want it to check the player to see if the player has a vehicle spawned anywhere in the map, and then destroy it, I also want it to be activated when the player spawns the car with the bind key, not when they hit the marker.

Server

	Spawn = createMarker (1682.8740234375, -2326.5634765625, 12.5, "cylinder", 1.5, 0, 50, 150, 200)
	VehiclesTable = {}
	
function ShowPanel (hitElement, matchingDimension)
	if isElement(hitElement) and getElementType(hitElement) == "player" and matchingDimension then
		local theVeh = getPedOccupiedVehicle(hitElement)
		if ( theVeh ) then 
			if ( VehiclesTable[hitElement] ) then 
		destroyElement(theVeh)
		VehiclesTable[hitElement] = nil
			end
		end
		if isPedOnGround ( hitElement ) then
			triggerClientEvent("showmenu", hitElement)
			bindKey ( hitElement, "1", "down", Sanchez )
		elseif not isPedOnGround ( hitElement ) then
			outputChatBox("You must be on foot!", hitElement, 230, 102, 103)
		end
	end
end

addEventHandler("onMarkerHit", Spawn, ShowPanel)


function Sanchez (hitElement)
	local x, y, z = getElementPosition (hitElement)
	local rotX, rotY, rotZ = getElementRotation (hitElement)
	VehiclesTable[hitElement] = createVehicle (468, x, y+5, z, rotX, rotY, 270)
	warpPedIntoVehicle (hitElement, VehiclesTable[hitElement])
end


function DestroyPanel (hitElement, matchingDimension)
	if isElement(hitElement) and getElementType(hitElement) == "player" and matchingDimension and isPedOnGround ( hitElement ) then
		triggerClientEvent("hidemenu", hitElement)
		unbindKey ( hitElement, "1", "down", Sanchez )
	end
end

addEventHandler("onMarkerLeave", Spawn, DestroyPanel)

The client stuff is just Dx window.

And my isPedOnGround isn't working. :/

Thanks for your help! :D

Link to comment

You have all your code in:

if (theVeh) then
  
end

This means, that your code will only run if the vehicle is existing. If the player is not in a vehicle then the vehicle can not exist. Also you put the player in the vehicles table instead of the vehicle.

What's more, you can set the vehicle element as element data on the player.

local veh = createVehicle(411, 0,0,5)
setElementData(player, "vehicle", veh)

 

Link to comment
14 hours ago, Randomly said:

You have all your code in:


if (theVeh) then
  
end

This means, that your code will only run if the vehicle is existing. If the player is not in a vehicle then the vehicle can not exist. Also you put the player in the vehicles table instead of the vehicle.

What's more, you can set the vehicle element as element data on the player.


local veh = createVehicle(411, 0,0,5)
setElementData(player, "vehicle", veh)

 

This only destroys the vehicle when the player drives into the marker, I want it to destroy the vehicle when a player spawns a new one from the marker... 

I don't really get what you are saying, do you mean I should get the player first, add them to the table, set the vehicle spawned to there data, and if they have that data to use that in destroyElement when they spawn new vehicle?

Edited by kieran
Link to comment
3 hours ago, kieran said:

This only destroys the vehicle when the player drives into the marker, I want it to destroy the vehicle when a player spawns a new one from the marker... 

I don't really get what you are saying, do you mean I should get the player first, add them to the table, set the vehicle spawned to there data, and if they have that data to use that in destroyElement when they spawn new vehicle?

Variable "theVeh" is the current vehicle of the player(getPedOccupiedVehicle). If the player is not in a vehicle, "theVeh" equals to nil and if (theVeh) then will be false, so the code in it wont run.

As i saw, you have put the hit element in the vehicles table, which, in the case if you are walking into the marker, is the player.

Link to comment
29 minutes ago, Randomly said:

Variable "theVeh" is the current vehicle of the player(getPedOccupiedVehicle). If the player is not in a vehicle, "theVeh" equals to nil and if (theVeh) then will be false, so the code in it wont run.

As i saw, you have put the hit element in the vehicles table, which, in the case if you are walking into the marker, is the player.

Yes, I took it out, but I do not know how to get the spawned vehicle when the player goes back into the marker, I never needed to destroy vehicles that are NOT occupied, so here is the very bad code I tried, it says it couldn't get the element from getElementData.

	Spawn = createMarker (1682.8740234375, -2326.5634765625, 12.5, "cylinder", 1.5, 0, 50, 150, 200)
	vehicle = {}

function ShowPanel (hitElement, matchingDimension)
	if isElement(hitElement) and getElementType(hitElement) == "player" and matchingDimension then
		if not isPedOnGround ( hitElement ) then
			outputChatBox("You must be on foot!", hitElement, 230, 102, 103)
		end
		local theVeh = getElementData( hitElement, "freecar" )
			if ( theVeh ) == veh then 
				if ( vehicle ) then 
				destroyElement(theVeh)
				vehicle = nil
				setElementData(hitElement, "freecar", nil)
				end
			end	
		triggerClientEvent("showmenu", hitElement)
		bindKey ( hitElement, "1", "down", Sanchez )
	end
end

addEventHandler("onMarkerHit", Spawn, ShowPanel)


function Sanchez (hitElement)
	local x, y, z = getElementPosition (hitElement)
	local rotX, rotY, rotZ = getElementRotation (hitElement)
	vehicle = createVehicle (468, x, y+5, z, rotX, rotY, 270)
	warpPedIntoVehicle (hitElement, vehicle)
	setElementData(hitElement, "freecar", veh)
end


function DestroyPanel (hitElement, matchingDimension)
	if isElement(hitElement) and getElementType(hitElement) == "player" and matchingDimension then
		triggerClientEvent("hidemenu", hitElement)
		unbindKey ( hitElement, "1", "down", Sanchez )
	end
end

addEventHandler("onMarkerLeave", Spawn, DestroyPanel)

Say I have a bike spawned from the marker in the water, and I go back and spawn another, the bike in the water that the player spawned will be destroyed, but it won't destroy other vehicles that other players have spawned from the marker.

Edited by kieran
Link to comment
	Spawn = createMarker (1682.8740234375, -2326.5634765625, 12.5, "cylinder", 1.5, 0, 50, 150, 200)
	vehicle = {}

function ShowPanel (hitElement, matchingDimension)
	if isElement(hitElement) and getElementType(hitElement) == "player" and matchingDimension then
		if not isPedOnGround ( hitElement ) then -- to check if the player is in a vehicle, you can use getPedOccupiedVehicle(). It returns false if the player is not in a vehicle.
			outputChatBox("You must be on foot!", hitElement, 230, 102, 103)
      		return -- This will make sure, that if the player is not on foot, the code will not run further.
		end
		local theVeh = getElementData( hitElement, "freecar" )
		if ( theVeh ~= false ) then -- You checked if "theVeh" is equal to "veh" but I can't find the "veh" variable in this code block. 
			destroyElement(theVeh)
			theVeh = nil -- With "vehicle = nil" you erased your table, defined above. This line, by the way, is not really neccessary.
			setElementData(hitElement, "freecar", false) -- I changed the nil to false.
		end
		triggerClientEvent(hitElement, "showmenu", resourceRoot) -- triggerClientEvent(elementToSend, eventToTrigger, sourceElement, additional, arguments)
		bindKey ( hitElement, "1", "down", Sanchez, hitElement) -- Added the hitElement argument, so the Sanchez function actually gets it.
	end
end

addEventHandler("onMarkerHit", Spawn, ShowPanel)


function Sanchez (hitElement) -- You have to send that "hitElement" to this function, in this case, we do it as an argument in bindKey.
	local x, y, z = getElementPosition (hitElement)
	local rotX, rotY, rotZ = getElementRotation (hitElement)
	veh = createVehicle (468, x, y+5, z, rotX, rotY, 270) -- You used the variable "vehicle" here. You have a table named "vehicle" so this can't be that. I fixed it to "veh"
	warpPedIntoVehicle (hitElement, veh)
	setElementData(hitElement, "freecar", veh) -- BUT here you used the "veh" variable, which in your case is nil.
end


function DestroyPanel (hitElement, matchingDimension)
	if isElement(hitElement) and getElementType(hitElement) == "player" and matchingDimension then
		triggerClientEvent(hitElement, "hidemenu", resourceRoot)
		unbindKey ( hitElement, "1", "down", Sanchez )
	end
end

addEventHandler("onMarkerLeave", Spawn, DestroyPanel)

It should work. I DID NOT test it, so it can have syntax errors, but this should work.

Edited by Randomly
Fix
Link to comment

@Randomly Thanks so much, this works brilliant :) I understand it a bit better now, although unsure if it will work for many players as I'm doing it solo until I get an emulator on my linux PC. :)

Well, it all works but the isPedOnGround output...  But that's not important, important thing is that it's limited 1 vehicle per player now :)

Edited by kieran
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...