Jump to content

[Important-Help]Respawn Vehicle


amirmahdi

Recommended Posts

8 hours ago, amirmahdi said:

When the car falls into the water, move the car to the nearest land?

You should be able to solve the first part of the question by yourself using the same approach that you used for the player but now based on the player's vehicle.

To implement the second part you need to know about safe coordinates for vehicle placement that are not inside water and connect that to the first part of the question using the setElementPosition function. Since MTA by default does not provide you with a function to retrieve recommended land positions I suggest you to try taking the traffic path nodes from the MTA community traffic resource. The path node definitions are located in the definitions folder. The most interesting function inside of the resource is the following (traffic_common.lua):

function pathsNodeFindClosest ( x, y, z )
	local areaID = getAreaFromPos ( x, y, z )
	local minDist, minNode
	local nodeX, nodeY, dist
	-- for id,node in pairs( AREA_PATHS[areaID] ) do
	for id,node in pairs( AREA_PATHS_ALL[areaID].veh ) do
		nodeX, nodeY = node.x, node.y
		dist = (x - nodeX)*(x - nodeX) + (y - nodeY)*(y - nodeY)
		if not minDist or dist < minDist then
			minDist = dist
			minNode = node
		end
	end
	return minNode
end

It is a pretty icky mathematical solution to your problem because this function does not return a point on the path lines but just the points that are stored in the database as start and end points of lines, but it is better than having nothing! Use this function by passing the vehicle position inside of the water to it and it will return a node object with fields x, y, and z.

You will have to find a ground position based on the point above ground (for ex. the node x, y and z). I suggest you use the getGroundPosition function. A popular candidate for calculating the ground-offset of vehicles is the getElementDistanceFromCentreOfMassToBaseOfModel function. Just add this distance to the ground z coordinate and you should obtain a good vehicle placement position based on the 0, 0, 0 euler rotation.

You will have to slice parts of the resource away and put them into your own because there are no exported functions for the traffic resource.

Have fun!

Edited by The_GTA
  • Thanks 1
Link to comment
6 hours ago, The_GTA said:

You should be able to solve the first part of the question by yourself using the same approach that you used for the player but now based on the player's vehicle.

To implement the second part you need to know about safe coordinates for vehicle placement that are not inside water and connect that to the first part of the question using the setElementPosition function. Since MTA by default does not provide you with a function to retrieve recommended land positions I suggest you to try taking the traffic path nodes from the MTA community traffic resource. The path node definitions are located in the definitions folder. The most interesting function inside of the resource is the following (traffic_common.lua):

function pathsNodeFindClosest ( x, y, z )
	local areaID = getAreaFromPos ( x, y, z )
	local minDist, minNode
	local nodeX, nodeY, dist
	-- for id,node in pairs( AREA_PATHS[areaID] ) do
	for id,node in pairs( AREA_PATHS_ALL[areaID].veh ) do
		nodeX, nodeY = node.x, node.y
		dist = (x - nodeX)*(x - nodeX) + (y - nodeY)*(y - nodeY)
		if not minDist or dist < minDist then
			minDist = dist
			minNode = node
		end
	end
	return minNode
end

It is a pretty icky mathematical solution to your problem because this function does not return a point on the path lines but just the points that are stored in the database as start and end points of lines, but it is better than having nothing! Use this function by passing the vehicle position inside of the water to it and it will return a node object with fields x, y, and z.

You will have to find a ground position based on the point above ground (for ex. the node x, y and z). I suggest you use the getGroundPosition function. A popular candidate for calculating the ground-offset of vehicles is the getElementDistanceFromCentreOfMassToBaseOfModel function. Just add this distance to the ground z coordinate and you should obtain a good vehicle placement position based on the 0, 0, 0 euler rotation.

You will have to slice parts of the resource away and put them into your own because there are no exported functions for the traffic resource.

Have fun!

Oh man I'm confused? (Thank you for all your help)

It can be easily soured without worries

For example, define coordinates in a table where the system remembers which coordinates are close to the car in the water.

Can you tell me the code?

Link to comment
11 hours ago, The_GTA said:

Have you downloaded the traffic resource from MTA community? Take a look into it. It has got the things you need.

Oh how dumb I am. Thankful

I need another little help

This is the client side code

What can I do to avoid the following warning?

WARNING: testres\c.lua:4:Bad argument @'getVehicleType'[Expected number at argument 1,got boolean]

addEventHandler( "onClientRender", root,
function()
	local theVehicle = getPedOccupiedVehicle(getLocalPlayer())
	local vehType = getVehicleType(theVehicle) -- Warning is Here
	if vehType == "Plane" or vehType == "Helicopter" then
		showfor = false
    	end
    	--Info : The code continues from here and includes dxDraw functions That's why onClientRender is used
end)

 

Edited by amirmahdi
Link to comment
4 hours ago, amirmahdi said:

What can I do to avoid the following warning?

WARNING: testres\c.lua:4:Bad argument @'getVehicleType'[Expected number at argument 1,got boolean]

The easiest and recommended way to check for validity of the theVehicle variable is to ask the Lua runtime whether theVehicle is not nil and not false. You can do that with a simple if-condition where the condition consists of the variable only. Like this:

...
    local theVehicle = getPedOccupiedVehicle(getLocalPlayer())
    if (theVehicle) then
        local vehType = getVehicleType(theVehicle) -- Warning is Here
...

This check is sufficient due to the documentation of the getPedOccupiedVehicle function. It says that it returns false only if the player is not inside of a vehicle. Else it returns a vehicle element which is never false and never nil.

Don't worry about yourself. You seem to be progressing in the right direction. ?

Edited by The_GTA
  • Thanks 1
Link to comment
28 minutes ago, The_GTA said:

The easiest and recommended way to check for validity of the theVehicle variable is to ask the Lua runtime whether theVehicle is not nil and not false. You can do that with a simple if-condition where the condition consists of the variable only. Like this:

...
    local theVehicle = getPedOccupiedVehicle(getLocalPlayer())
    if (theVehicle) then
        local vehType = getVehicleType(theVehicle) -- Warning is Here
...

This check is sufficient due to the documentation of the getPedOccupiedVehicle function. It says that it returns false only if the player is not inside of a vehicle. Else it returns a vehicle element which is never false and never nil.

Don't worry about yourself. You seem to be progressing in the right direction. ?

Thank you very much for everything

Bravo

Link to comment
1 minute ago, amirmahdi said:

Thank you very much for everything

Bravo

You're welcome, friend! I really enjoy people like you who are willing to go on this difficult journey to learn programming. May you have a good future inside of the MTA community!

Edited by The_GTA
  • Thanks 1
Link to comment
On 20/10/2021 at 03:04, The_GTA said:

Hello amirmahdi,

this is done in two simple steps. (serverside)

  • First we calculate the nearest vehicle of the player. This can be done by getting all vehicles on the server and using the distance metric to find the vehicle with the smallest distance and return it.
local function findClosestVehicle(elem)
    local vehicles = getElementsByType("vehicle");
    
    local elem_x, elem_y, elem_z = getElementPosition(elem);
    
    local closest_veh = false;
    local closest_dist = false;
    
    for m,veh in ipairs(vehicles) do
        local veh_x, veh_y, veh_z = getElementPosition(veh);
        
        local dist = getDistanceBetweenPoints3D(elem_x, elem_y, elem_z, veh_x, veh_y, veh_z);
        
        if not (closest_dist) or (closest_dist > dist) then
            closest_dist = dist;
            closest_veh = veh;
        end
    end
    
    return closest_veh;
end
  • Next we create a command handler "rscar" which uses our new function to respawn the closest vehicle.
addCommandHandler("rscar",
    function(player)
        local veh = findClosestVehicle(player);
        if not (veh) then
            outputChatBox("could not find close vehicle", player);
            return;
        end
        -- TODO: use respawnVehicle to place the vehicle on the map again.
    end
);

Good luck!

Hello buddy
This can be done
Fix only cars that are half a meter away from us?

Link to comment
  • 3 weeks later...
On 11/11/2021 at 01:36, The_GTA said:

You're welcome, friend! I really enjoy people like you who are willing to go on this difficult journey to learn programming. May you have a good future inside of the MTA community!

Hi Again

I had a strange problem

Bad argument @ 'getResourceRootElement' [Expected resource-data at argument 1] [string "?"]

 

Code : ( serverside )

local invSys = exports["inv"]
function whenPlayerLeft()  
	invSys:setInvData(getPlayerName(source),"i1",getElementData(source,"invs1")) -- Warning is Here
end
addEventHandler("onPlayerQuit", getRootElement(), whenPlayerLeft)

I did not use the getResourceRootElement function but it warns ?

Link to comment
  • 2 weeks later...
On 29/11/2021 at 04:38, The_GTA said:

You're welcome! I see that you are not opening up new threads for your inquiries. Personally I do not mind it.

Hello buddy

I want to give Health to other Players  on the server by pressing a button. Of course, only players that are close to us.

 

And that I did this using the previous code you provided.

 

But this is the problem.

I want this to be done only for other people That means we should not give ourselves health

 

Code

local function findClosestplayer(elem)
    local vehicles = getElementsByType("player");
    local elem_x, elem_y, elem_z = getElementPosition(elem);
    local closest_veh = false;
    local closest_dist = false;
    for m,veh in ipairs(vehicles) do
        local veh_x, veh_y, veh_z = getElementPosition(veh);
        local dist = getDistanceBetweenPoints3D(elem_x, elem_y, elem_z, veh_x, veh_y, veh_z);
        if not (closest_dist) or (closest_dist > dist) then
            closest_dist = dist;
            closest_veh = veh;
        end
    end
    return closest_veh;
end
function medic (thePlayer)
    local veh = findClosestplayer(thePlayer);
    if veh then
    local x1,y1,z1 = getElementPosition(thePlayer)
    local x2,y2,z2 = getElementPosition(veh)
    local distance = getDistanceBetweenPoints3D(x1,y1,z1,x2,y2,z2)
        if tonumber(distance) < 2.5 then
            setElementHealth(veh,100)
        end
    end
end

 

Edited by amirmahdi
Link to comment
1 hour ago, amirmahdi said:

I want this to be done only for other people That means we should not give ourselves health

Welcome back, amirmahdi.

If you want to search for a player B who is closest to player A but A should not be equal to B then you have to tell the code that it should skip A when looking for closest players. Otherwise it will always find A because A is always closest player to A.

    ...
    for m,veh in ipairs(vehicles) do
        if not (veh == elem) then -- exclude the player whose closest players we want to find
            local veh_x, veh_y, veh_z = getElementPosition(veh);
    ...

Please rename the variables inside of the function so that they match the element type to prevent confusion.

  • Thanks 1
Link to comment
On 08/12/2021 at 13:42, The_GTA said:

Welcome back, amirmahdi.

If you want to search for a player B who is closest to player A but A should not be equal to B then you have to tell the code that it should skip A when looking for closest players. Otherwise it will always find A because A is always closest player to A.

    ...
    for m,veh in ipairs(vehicles) do
        if not (veh == elem) then -- exclude the player whose closest players we want to find
            local veh_x, veh_y, veh_z = getElementPosition(veh);
    ...

Please rename the variables inside of the function so that they match the element type to prevent confusion.

Hello again ?

Is there a way to show the cars that exploded on the map?

For example, the following code shows players that have Wanted !

...
for i, v in ipairs(getElementsByType("player")) do
            if getElementHealth(v) < 100 then 
                if getElementData(v,"loggedIn") then 
                    blips = createBlipAttachedTo( v, 0, 2, 255, 255, 100, 100 ) 
                    setElementVisibleTo(blips, getRootElement(), false)
                    setElementVisibleTo(blips, thePlayer, true)
                    setElementData(thePlayer,"jobs:police",blips)
                end
            end
        end
...

This is just one problem with this example

When the Wanted player is gone, it will still be displayed on the map (can this be fixed?)

Edited by amirmahdi
Link to comment
32 minutes ago, amirmahdi said:

When the Wanted player is gone, it will still be displayed on the map (can this be fixed?)

Hello amirmahdi,

I assume that every player can have a blip attached to it which is stored in the "jobs:police" element data slot of the player element. I can see that it is a server-side script because you are using the setElementVisibleTo function. By "gone" you mean that the player has left the server, right? For any kind of "gone" relationship where an element is being destroyed and you have to clean up any associated that with the to-be-destroyed element, you should use the "onElementDestroy" server-side event handler. For example, you can clean up the blip like this:

addEventHandler("onElementDestroy", root,
    function()
        if (getElementType(source) == "player") then
            local wanted_blip = getElementData(source, "jobs:police");
            if (wanted_blip) then
                destroyElement(wanted_blip);
                removeElementData(source, "jobs:police");
            end
        end
    end
);

Please make sure that you are not creating a second blip and overwritting the "jobs:police" element data with the element still present in your code. After all, you are not checking whether "jobs:police" element data is already set with a blip before creating and assigning a new one. Doing this the wrong way would create multiple blips that overlap the original one and you would not be able to easily destroy these extra and unnecessary ones.

Good luck! ?

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

Hello amirmahdi,

I assume that every player can have a blip attached to it which is stored in the "jobs:police" element data slot of the player element. I can see that it is a server-side script because you are using the setElementVisibleTo function. By "gone" you mean that the player has left the server, right? For any kind of "gone" relationship where an element is being destroyed and you have to clean up any associated that with the to-be-destroyed element, you should use the "onElementDestroy" server-side event handler. For example, you can clean up the blip like this:

addEventHandler("onElementDestroy", root,
    function()
        if (getElementType(source) == "player") then
            local wanted_blip = getElementData(source, "jobs:police");
            if (wanted_blip) then
                destroyElement(wanted_blip);
                removeElementData(source, "jobs:police");
            end
        end
    end
);

Please make sure that you are not creating a second blip and overwritting the "jobs:police" element data with the element still present in your code. After all, you are not checking whether "jobs:police" element data is already set with a blip before creating and assigning a new one. Doing this the wrong way would create multiple blips that overlap the original one and you would not be able to easily destroy these extra and unnecessary ones.

Good luck! ?

Thankful

Is there just a way to show the location of the cars that exploded on the map? (As a blip)

Link to comment
18 hours ago, The_GTA said:

Hello amirmahdi,

I assume that every player can have a blip attached to it which is stored in the "jobs:police" element data slot of the player element. I can see that it is a server-side script because you are using the setElementVisibleTo function. By "gone" you mean that the player has left the server, right? For any kind of "gone" relationship where an element is being destroyed and you have to clean up any associated that with the to-be-destroyed element, you should use the "onElementDestroy" server-side event handler. For example, you can clean up the blip like this:

addEventHandler("onElementDestroy", root,
    function()
        if (getElementType(source) == "player") then
            local wanted_blip = getElementData(source, "jobs:police");
            if (wanted_blip) then
                destroyElement(wanted_blip);
                removeElementData(source, "jobs:police");
            end
        end
    end
);

Please make sure that you are not creating a second blip and overwritting the "jobs:police" element data with the element still present in your code. After all, you are not checking whether "jobs:police" element data is already set with a blip before creating and assigning a new one. Doing this the wrong way would create multiple blips that overlap the original one and you would not be able to easily destroy these extra and unnecessary ones.

Good luck! ?

In the following code, the player executes 'setTimer' by pressing the E button

...
setTimer(showvehexplodedordamaged,1000,1,thePlayer)
...

And executes the following code The function of this code is to show damaged or exploded cars on the map

function showvehexplodedordamaged (thePlayer)
    for i, v in ipairs(getElementsByType("vehicle")) do
        if getElementHealth(v) < 1000 then 
                blips = createBlipAttachedTo( v, 0, 2, 50, 50, 255, 255 ) 
                setElementVisibleTo(blips, getRootElement(), false)
                setElementVisibleTo(blips, thePlayer, true)
        end
    end
end

So far, our code is doing its job and there is no problem

 

We said that this code shows damaged or exploded cars on the map

But the problem is that if the damaged or exploded car is fixed, it still shows it on the map !!!

How do I solve this problem?

Edited by amirmahdi
Link to comment
5 minutes ago, The_GTA said:
  • stop the blip timer once the vehicle is fixed so it does not trigger
  • clean up any blip that was created for the vehicle

Excuse me buddy my English is week And I use the translator most of the time Is it possible to code the steps that explain the video to me?

Link to comment
1 minute ago, amirmahdi said:

Excuse me buddy my English is week And I use the translator most of the time Is it possible to code the steps that explain the video to me?

Get yourself an idea how to solve the problem by these posts:

Fixing a vehicle involves a call to fixVehicle or any other combination of vehicle state resetting functions. Consult the wiki for details on how to fix a vehicle and then combine the fixing of a vehicle with the things I suggested you earlier.

If English is not a language you can understand then please feel free to switch to a language section that fits you best. While the reply speed may not be as fast as me, I am pretty sure that we have competent people who can support you natively because the boards would not exist in the first place if it were not true.

  • Thanks 1
Link to comment
On 12/12/2021 at 14:52, The_GTA said:

Get yourself an idea how to solve the problem by these posts:

Fixing a vehicle involves a call to fixVehicle or any other combination of vehicle state resetting functions. Consult the wiki for details on how to fix a vehicle and then combine the fixing of a vehicle with the things I suggested you earlier.

If English is not a language you can understand then please feel free to switch to a language section that fits you best. While the reply speed may not be as fast as me, I am pretty sure that we have competent people who can support you natively because the boards would not exist in the first place if it were not true.

Hello The_GTA

I finally solved the previous problem Thanks so much for the tips

Is it possible to show the blip only for a specified team?

Like the code below But the problem is that the clip is not made at all Or if it happens, it is not visible to anyone

function test()
    if getElementData(source,"vehdamyu") == false then 
        setElementData(source,"vehdamyu",true)
        blipes = createBlipAttachedTo( source, 0, 2, 50, 50, 255, 255 )  --This
    	setElementVisibleTo(blipes,getRootElement(),false) -- this
        setElementVisibleTo(blipes,getTeamFromName("Police"),true) -- This
    end
end

addEventHandler("onVehicleDamage", root, test)

 

Edited by amirmahdi
Link to comment
10 hours ago, amirmahdi said:

Is it possible to show the blip only for a specified team?

Hello amirmahdi,

if a player is assigned to a team then a hidden "team" reference is set to the requested team object. The element hierarchy is not changed. Players are not children of team elements. The setElementVisibleTo function does not change the visibility of the player because it is not a child of the team element.

Instead you can use the getPlayersInTeam function in connection with a for-loop to call the setElementVisibleTo function on all players...

...
    	setElementVisibleTo(blipes,getRootElement(),false) -- this
        for _,p in ipairs(getPlayersInTeam(getTeamFromName("Police"))) do
            setElementVisibleTo(blipes,p,true) -- This
        end
    end
...

Hope this helps!

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