Jump to content

DestroyElement Problem :( (Not destroying the Element or destroying the incorrect element)


Andres99907

Recommended Posts

Im working on a script wich create flames on the Thrusters of a vehicle (Mammoth Thruster of GTA Online), works well if you are the only that has a Thruster, but if someone also has a Thruster and gets out the vehicle, your Thruster will keep the attached elements and will not destroy the flames if you get out of the vehicle:

Here is a video of what is happening:

https://www.youtube.com/watch?v=Q3z9fdnBG0w

and serverside and clientside scripts that are involved :(

Serverside:             (Functions for creating the flames)

function LightOn(vehicle, x, y, z)
if thePlayer ~= localPlayer then return end
light = createMarker ( x , y , z, "corona", 0.5, 255, 160, 0, 170 )
attachElements(light, vehicle, 0, -0.3, -0.8)
end
addEvent("lighton", true)
addEventHandler("lighton", getRootElement(), LightOn)

function LightOff()
if thePlayer ~= localPlayer then return end
if light then
destroyElement(light)
   end
end
addEvent("lightoff", true)
addEventHandler("lightoff", getRootElement(), LightOff)

function ThrustersOn(vehicle, x, y, z)
if thePlayer ~= localPlayer then return end
superhitbox = createObject (3471, x , y , z)
setElementAlpha(superhitbox, 0)
firethrustl = createObject (2031, x , y , z)
firethrustr = createObject (2031, x , y , z)
attachElements(superhitbox, vehicle, 0, -10, -1)
attachElements(firethrustl, vehicle, 0.47, -0.4, -0.2)
attachElements(firethrustr, vehicle, -0.47, -0.4, -0.2)
setElementCollisionsEnabled(firethrustl, false)
setElementCollisionsEnabled(firethrustr, false)
end
addEvent("thrusterson", true)
addEventHandler("thrusterson", getRootElement(), ThrustersOn)

function ThrustersOff()
if thePlayer ~= localPlayer then return end
if not firethrustl then return end
destroyElement(firethrustl)
destroyElement(firethrustr)
end
addEvent("thrustersoff", true)
addEventHandler("thrustersoff", getRootElement(), ThrustersOff)

Clientside: (Functions that triggers serverside events when you are in the Thruster)

Thruster = 465

function SitOnThruster(thePlayer)
    if thePlayer ~= localPlayer then return end
	local vehicle = getPedOccupiedVehicle(localPlayer)
		if ( vehicle and getElementModel (vehicle)  == Thruster ) then
		bindKey("vehicle_fire", "down", shootProjectile)
		triggerServerEvent ( "onthruster", resourceRoot)
		local x, y, z = getElementPosition(vehicle)
		local h, m = getTime()
		triggerServerEvent("thrusterson", getRootElement(), vehicle, x, y, z)
		if h > 20 then
		triggerServerEvent("lighton", getRootElement(), vehicle, x, y, z)
		else return end
	end
 end

addEventHandler ( "onClientVehicleEnter", root, SitOnThruster )

function Notsit(thePlayer)
        if thePlayer ~= localPlayer then return end
        if Thruster then
		triggerServerEvent("thrustersoff", getRootElement(), vehicle, x, y, z)
		triggerServerEvent("lightoff", getRootElement(), vehicle, x, y, z)
		else return end
	end

addEventHandler ( "onClientVehicleExit", root, Notsit )

function killed()
        if Thruster then
		if (isPedInVehicle(localPlayer) and Cars[getElementModel(getPedOccupiedVehicle(localPlayer))]) then
		if getPedOccupiedVehicleSeat ( localPlayer ) == 1 then return end
		triggerServerEvent("thrustersoff", getRootElement(), vehicle, x, y, z)
		triggerServerEvent("lightoff", getRootElement(), vehicle, x, y, z)
		else return end
	end
end

addEventHandler ( "onClientPlayerWasted", localPlayer, killed )

function shootProjectile()
    if not disparado then
	local vehicle = getPedOccupiedVehicle(localPlayer)
	if ( vehicle and getElementModel (vehicle)  == Thruster ) then
	if isVehicleOnGround(vehicle) == false then
		local x, y, z = getElementPosition(vehicle)
		projectile = createProjectile(vehicle, 15, x, y+4, z-10)
	disparado = true
	setTimer ( function()
	disparado = false
	end, 1500, 1 )	
	else
    	local x, y, z = getElementPosition(vehicle)
		createProjectile(vehicle, 15, x, y, z-3)
	disparado = true
	setTimer ( function()
	disparado = false
	end, 1500, 1 )
	  end
    end
  end
end

:( I'm very confused, i'm trying to make the script only create element for each player.

Link to comment

The reason is that your script only has storage for one flames element, while there can be multiple people. What you need to do is use tables, with the vehicle element as the key, and its flames element as the value. That way, you index the table to get the element, check if it exists, delete it if so, create new one if necessary, etc.

Here, I've fixed one of the functions for you as an example, and you should be able to

-- beginning of serverside code
firethrusters = {
  --structure: [vehicle] = {[1] = firethrustl, [2] = firethrustr}
}

-- ... the rest of the code here ...

function ThrustersOff(veh, x, y, z) -- you forgot to collect the parameters the event was sending over
  --if thePlayer ~= localPlayer then return end -- no idea what this line does. localPlayer isn't defined on the server, neither is thePlayer in this function
  if not firethrusters[veh] then return end -- if there is no firethruster for this vehicle, abort here
  destroyElement(firethrusters[veh][1])
  destroyElement(firethrusters[veh][2])
  firethrusters[veh] = nil -- presence of this data determines whether an attempt will be made to delete, remove this data to prevent potential 'nil passed instead of element to destroyElement' error
end
addEvent("thrustersoff", true)
addEventHandler("thrustersoff", getRootElement(), ThrustersOff)

You'll need to do the same for the light. You'll also want to handle the event onElementDestroy to catch the vehicle disappearing for whatever reason, and make sure these thruster elements and stuff are also deleted, otherwise they'll remain in the place where the vehicle last was, forever.

Edited by Addlibs
  • Like 1
Link to comment
On 19/12/2020 at 04:04, Addlibs said:

The reason is that your script only has storage for one flames element, while there can be multiple people. What you need to do is use tables, with the vehicle element as the key, and its flames element as the value. That way, you index the table to get the element, check if it exists, delete it if so, create new one if necessary, etc.

Here, I've fixed one of the functions for you as an example, and you should be able to


-- beginning of serverside code
firethrusters = {
  --structure: [vehicle] = {[1] = firethrustl, [2] = firethrustr}
}

-- ... the rest of the code here ...

function ThrustersOff(veh, x, y, z) -- you forgot to collect the parameters the event was sending over
  --if thePlayer ~= localPlayer then return end -- no idea what this line does. localPlayer isn't defined on the server, neither is thePlayer in this function
  if not firethrusters[veh] then return end -- if there is no firethruster for this vehicle, abort here
  destroyElement(firethrusters[veh][1])
  destroyElement(firethrusters[veh][2])
  firethrusters[veh] = nil -- presence of this data determines whether an attempt will be made to delete, remove this data to prevent potential 'nil passed instead of element to destroyElement' error
end
addEvent("thrustersoff", true)
addEventHandler("thrustersoff", getRootElement(), ThrustersOff)

You'll need to do the same for the light. You'll also want to handle the event onElementDestroy to catch the vehicle disappearing for whatever reason, and make sure these thruster elements and stuff are also deleted, otherwise they'll remain in the place where the vehicle last was, forever.

That was a lot of work because i don't know much about tables, but i made the table and i made my script work :) thank you

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