Jump to content

Adding Heat-Seeking Missile to Hunter Helicopter


#MacTavish

Recommended Posts

Hi all, (first im sorry for my bad english) , i want to add heat-seeking missile to hunter helicopter and i want to fire heatseeking missile by Left shift button, but it doesn't work..Where am i wrong? (Hunter ID : 425) (Heat seeking ID : 20)

function shootProjectile()
    local vehicle = getPedOccupiedVehicle(localPlayer)
    if(vehicle) == 425 then
        local x, y, z = getElementPosition(vehicle)
        createProjectile(vehicle, 20, x, y, z)
    end
end

bindKey( "lshift", "down", shootProjectile)
addEventHandler("onClientResourceStart", resourceRoot, onResourceStart)

 

Link to comment
function shootProjectile()
    local vehicle = getPedOccupiedVehicle(localPlayer)
    if getElementModel(vehicle) == 425 then
        local x, y, z = getElementPosition(vehicle)
        createProjectile(vehicle, 20, x, y, z)
    end
end

bindKey( "lshift", "down", shootProjectile)
addEventHandler("onClientResourceStart", resourceRoot, onResourceStart)

I can fire heat-seeking missile from Hunter Helicopter by this script, but its like normal missile. I mean, i can't lock it on a target, what should i do..

Link to comment
projectile createProjectile ( element creator, int weaponType [, float posX, float posY, float posZ, float force = 1.0, element target = nil, float rotX, float rotY, float rotZ, float velX, float velY, float velZ, int model ] )

You need to specify its 7th argument - target

Edited by Bilal135
  • Like 1
Link to comment
4 hours ago, Bilal135 said:

projectile createProjectile ( element creator, int weaponType [, float posX, float posY, float posZ, float force = 1.0, element target = nil, float rotX, float rotY, float rotZ, float velX, float velY, float velZ, int model ] )

You need to specify its 7th argument - target

Thank you again Bilal, i will try it and i will notify you 

Link to comment
function shootProjectile()
	local vehicle = getPedOccupiedVehicle(localPlayer)
	if vehicle and getElementModel(vehicle) == 425 then
		local target = getElementModel(targetvehicle) or nil
		local x, y, z = getElementPosition(vehicle)
		createProjectile(vehicle, 20, x, y, z, target)
	end
end

bindKey( "lshift", "down", shootProjectile)

I scripted this , i can fire heatseeking missile from Hunter but... still can't lock on a target, what should i do.. thanks

Link to comment

hey all, can somebody help me , i want to fire heat-seeking missile on a "target". I can fire heatseeking missile but can't lock it on a "target", please help, thanks 

function shootProjectile()
	
	local hunter = getPedOccupiedVehicle(localPlayer)
	if hunter and getElementModel(hunter) == 425 then
		local x, y, z = getElementPosition(hunter)
		createProjectile(hunter, 20, x, y, z, hedef)
		local hedef = getElementsWithinRange(x, y, z, 300, "vehicle")
	end
end

bindKey( "lshift", "down", shootProjectile)

I almost forgot, HAPPY NEW YEAR btw !

Edited by #MacTavish
Link to comment
local vehicleID = 425;
local projectileType = 20;
local targetRange = 300;

function shootProjectile()
    -- Ensure player is in a vehicle before trying to get the vehicle of player.
	if isPedInVehicle(localPlayer) then
	    local vehicle = getPedOccupiedVehicle(localPlayer)
	    if (vehicle and getElementModel(vehicle) == vehicleID) then
            local x, y, z = getElementPosition(vehicle)
            local targets = getElementsWithinRange(x, y, z, targetRange, "vehicle")
            -- Returns a table containing all the elements of the specified type within range.
            for k, v in pairs(targets) do
                -- Loop through the table.
                if v and isElement(v) then
                    -- Here 'v' are our targets. Check if exists.
                    createProjectile(vehicle, projectileType, x, y, z, v)
                    -- Create projectiles that will target all of the players within targetRange.
                end
            end
        end
    else
        return false
    end
end
bindKey( "lshift", "down", shootProjectile)

 

Edited by Bilal135
Link to comment
7 hours ago, Bilal135 said:

local vehicleID = 425;
local projectileType = 20;
local targetRange = 300;

function shootProjectile()
    -- Ensure player is in a vehicle before trying to get the vehicle of player.
	if isPedInVehicle(localPlayer) then
	    local vehicle = getPedOccupiedVehicle(localPlayer)
	    if (vehicle and getElementModel(vehicle) == vehicleID) then
            local x, y, z = getElementPosition(vehicle)
            local targets = getElementsWithinRange(x, y, z, targetRange, "vehicle")
            -- Returns a table containing all the elements of the specified type within range.
            for k, v in pairs(targets) do
                -- Loop through the table.
                if v and isElement(v) then
                    -- Here 'v' are our targets. Check if exists.
                    createProjectile(vehicle, projectileType, x, y, z, v)
                    -- Create projectiles that will target all of the players within targetRange.
                end
            end
        end
    else
        return false
    end
end
bindKey( "lshift", "down", shootProjectile)

 

Thank you so much Bilal135, i tried your script but i got debug in 17th line 6th argument (it says got table). I created another script with some help that works really good, here is the script if you want to use :)   ;

function shootProjectile()
    local vehicle = getPedOccupiedVehicle(localPlayer)
	if vehicle and getElementModel(vehicle) == 425 then
		local x, y, z = getElementPosition(getLocalPlayer())
		local vehicles = getElementsWithinRange(x, y, z, 300, "vehicle")
    	local target = vehicles[1] ~= vehicle and vehicles[1] or vehicles[2]
		
		if target then 
			createProjectile(vehicle, 20, x, y, z, 1, target)
		end
	end
end
bindKey( "lshift", "down", shootProjectile)

Btw i don't understand what are they for [1] and [2], what does they mean? can you explain me please if you know, thanks

Link to comment

To understand what [1] and [2] are, first you must understand the basics of table.

A table is like a container and you can put a lot of things in it, that can be accessed later on. For example,

-- Example table
local table = {"John", "David", "Abraham", "Adam"}

You can insert any value in a table (e.g integers, strings etc). Every item in a table has an index value, which is basically an integer value that tells the position of its items. In this case,

local table = {"John", "David", "Abraham", "Adam"} 
-- John has index value '1' because it is the first item in our table
-- David's index value is 2, and so on.

When we refer to any particular item in a table, we use the following format, 

tableName[indexNumber];

For example, if we wanted to output the value of David, we would do this,

outputChatBox( table[2], sendToElement ) -- 'sendToElement' is the element which you want to send the message to, e.g root, source.

When we did the for loop in the above source code, we actually looped through all the values in the table, e.g,

for index, value in ipairs(table) do
    outputChatBox(value, sendToElement)
end

This would output all the values in our table. In other words, we would see John, David, Abraham and Adam outputted in chat box.

In your source code, vehicles is a table that is returned by the function getElementsWithinRange, and obviously it has keys / indexes, and values. Values would be the player elements and keys / indexes would be the number that they are referred to in the table. 

local target = vehicles[1] ~= vehicle and vehicles[1] or vehicles[2]

What we are essentially checking here is that the target is not going to be ourselves but someone else, so the missiles don't actually target us. In other words, vehicles[1] is the first player element in the vehicles table. Check if vehicle[1] is not our own vehicle, which is defined above as, local vehicle = getPedOccupiedVehicle(localPlayer).

-------

About the debug error, it is because 'v' is not specifying an index i.e a player element, so if you replace it with 'v[1]', it should work. But then again, before doing this, add a check to ensure v[1] is not our own vehicle.

I'm glad you figured it out on your own. Good luck.

  • Thanks 1
Link to comment
1 hour ago, Bilal135 said:

To understand what [1] and [2] are, first you must understand the basics of table.

A table is like a container and you can put a lot of things in it, that can be accessed later on. For example,


-- Example table
local table = {"John", "David", "Abraham", "Adam"}

You can insert any value in a table (e.g integers, strings etc). Every item in a table has an index value, which is basically an integer value that tells the position of its items. In this case,


local table = {"John", "David", "Abraham", "Adam"} 
-- John has index value '1' because it is the first item in our table
-- David's index value is 2, and so on.

When we refer to any particular item in a table, we use the following format, 


tableName[indexNumber];

For example, if we wanted to output the value of David, we would do this,


outputChatBox( table[2], sendToElement ) -- 'sendToElement' is the element which you want to send the message to, e.g root, source.

When we did the for loop in the above source code, we actually looped through all the values in the table, e.g,


for index, value in ipairs(table) do
    outputChatBox(value, sendToElement)
end

This would output all the values in our table. In other words, we would see John, David, Abraham and Adam outputted in chat box.

In your source code, vehicles is a table that is returned by the function getElementsWithinRange, and obviously it has keys / indexes, and values. Values would be the player elements and keys / indexes would be the number that they are referred to in the table. 


local target = vehicles[1] ~= vehicle and vehicles[1] or vehicles[2]

What we are essentially checking here is that the target is not going to be ourselves but someone else, so the missiles don't actually target us. In other words, vehicles[1] is the first player element in the vehicles table. Check if vehicle[1] is not our own vehicle, which is defined above as, local vehicle = getPedOccupiedVehicle(localPlayer).

-------

About the debug error, it is because 'v' is not specifying an index i.e a player element, so if you replace it with 'v[1]', it should work. But then again, before doing this, add a check to ensure v[1] is not our own vehicle.

I'm glad you figured it out on your own. Good luck.

Im really thankful to you Bilal135, you are really helpful and good person ! Thank you

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